Skip to content

Commit

Permalink
added functions to deal with json/yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
knagadevara committed Jul 2, 2024
1 parent 515a6b7 commit c8987ed
Showing 1 changed file with 96 additions and 18 deletions.
114 changes: 96 additions & 18 deletions File.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goutility
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"os"
Expand All @@ -21,7 +22,7 @@ func LoadFile(flPth string) []byte {
}
}

// Open, Create, Read, Append
// Open, Create, Read, Append on a File
func OperateFile(fileName string, openPerms int, filePerms os.FileMode) *os.File {
file, err := os.OpenFile(fileName, openPerms, filePerms)
if err != nil {
Expand All @@ -30,6 +31,7 @@ func OperateFile(fileName string, openPerms int, filePerms os.FileMode) *os.File
return file
}

// Write the data to file.
func WriteToFile(file *os.File, resp io.Reader) int {
defer file.Close()
var writenSize int
Expand All @@ -46,19 +48,6 @@ func WriteToFile(file *os.File, resp io.Reader) int {
return writenSize
}

func DecodeFileToStruct[T any](file *os.File) T {
defer file.Close()
var result T
decodedJson := json.NewDecoder(bufio.NewReader(file))
for decodedJson.More() {
err := decodedJson.Decode(&result)
if err != nil {
return result
}
}
return result
}

// Check if file already exists
func CheckFileExists(fileName string) os.FileInfo {
fileInfo, err := os.Stat(fileName)
Expand All @@ -81,7 +70,7 @@ func CreateOrLoadData[T any](httpMethod, apiURL, fileName string) T {
if fileInfo != nil && fileInfo.Size() > 0 {

file = OperateFile(fileName, os.O_RDONLY, 0655)
return DecodeFileToStruct[T](file)
return RdJsonFileToStruct[T](file)

} else {

Expand All @@ -92,15 +81,104 @@ func CreateOrLoadData[T any](httpMethod, apiURL, fileName string) T {
file = OperateFile(fileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0655)
_ = WriteToFile(file, resp.Body)

return DecodeFileToStruct[T](file)
return RdJsonFileToStruct[T](file)
}
}

func YmlToStruct[T any](yamlBuf []byte) *T {
/*
#### E N C O D E #### * #### D E C O D E ####
*/

/* Y A M L */

// Yaml file to struct
func RdYamlFileToStruct[T any](file *os.File) T {
defer file.Close()
var result T
decodedYaml := yaml.NewDecoder(bufio.NewReader(file))
err := decodedYaml.Decode(&result)
if err != nil {
return result
}
return result
}

// Yaml buffer to struct
func RdYamlToStruct[T any](Buf []byte) *T {
var ymlStr T
err := yaml.Unmarshal(yamlBuf, &ymlStr)
err := yaml.Unmarshal(Buf, &ymlStr)
if err != nil {
log.Fatalln(err)
}
return &ymlStr
}

// Struct to Yaml File
func WrtStructToYamlFile[T any](CustStruct T, file *os.File) {
defer file.Close()
encodeYaml := yaml.NewEncoder(bufio.NewWriter(file))
defer encodeYaml.Close()
err := encodeYaml.Encode(CustStruct)
if err != nil {
fmt.Println("File Written!!")
} else {
log.Fatalln("Unable to Write!!", err)
}
}

// Struct to Yaml buffer
func WrtStructToYaml[T any](ymlStr T) []byte {
bArr, err := yaml.Marshal(ymlStr)
if err != nil {
log.Fatalln(err)
}
return bArr
}

/* J S O N */

// Json file to struct
func RdJsonFileToStruct[T any](file *os.File) T {
defer file.Close()
var result T
decodedJson := json.NewDecoder(bufio.NewReader(file))
for decodedJson.More() {
err := decodedJson.Decode(&result)
if err != nil {
return result
}
}
return result
}

// Json buffer to struct
func RdJsonToStruct[T any](Buf []byte) *T {
var jsnStr T
err := json.Unmarshal(Buf, &jsnStr)
if err != nil {
log.Fatalln(err)
}
return &jsnStr
}

// Struct to Json file
func WrtStructToJsonFile[T any](CustStruct T, file *os.File) {
defer file.Close()
encodedJson := json.NewEncoder(bufio.NewWriter(file))
err := encodedJson.Encode(&CustStruct)
if err != nil {
fmt.Println("File Written!!")
} else {
log.Fatalln("Unable to Write!!", err)
}
}

// Struct to Json buffer
func WrtStructToJson[T any](Buf T) []byte {
bArr, err := json.Marshal(&Buf)
// err := json.Unmarshal(Buf, &jsnStr)
if err != nil {
log.Fatalln(err)
}
return bArr
}

0 comments on commit c8987ed

Please # to comment.