Skip to content

Commit

Permalink
add toml helpers to read files into json bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
beckend committed May 24, 2021
1 parent 11c8fb3 commit 3fa1b70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package config
import (
errors "errors"
io "io"
fs "io/fs"
os "os"
path "path"
reflect "reflect"
Expand Down Expand Up @@ -32,10 +33,12 @@ type OnConfigBeforeValidationOptions struct {
}

type LoadConfigsOptionsTOML struct {
FileToJSON func(string) ([]byte, error)
StringToJSON func(string) ([]byte, error)
BytesToJSON func([]byte) ([]byte, error)
ReaderToJSON func(io.Reader) ([]byte, error)
FileToJSON func(string) ([]byte, error)
StringToJSON func(string) ([]byte, error)
BytesToJSON func([]byte) ([]byte, error)
ReaderToJSON func(io.Reader) ([]byte, error)
FileReaderToJSON func(file fs.File, closeFile bool) ([]byte, error)
FileReaderCallbackToJSON func(getFileCallback func() (fs.File, error)) ([]byte, error)
}

type LoadConfigsOptions struct {
Expand Down Expand Up @@ -87,10 +90,12 @@ func New(options *NewOptions) (*Config, error) {
if options.LoadConfigs != nil {
byteSlicesUser, err := options.LoadConfigs(&LoadConfigsOptions{
TOML: &LoadConfigsOptionsTOML{
FileToJSON: file.TOMLFileToJSON,
BytesToJSON: file.TOMLBytesToJSON,
StringToJSON: file.TOMLStringToJSON,
ReaderToJSON: file.TOMLReaderToJSON,
FileToJSON: file.TOMLFileToJSON,
BytesToJSON: file.TOMLBytesToJSON,
StringToJSON: file.TOMLStringToJSON,
ReaderToJSON: file.TOMLReaderToJSON,
FileReaderToJSON: file.TOMLFileReaderToJSON,
FileReaderCallbackToJSON: file.TOMLFileReaderCallbackToJSON,
},
RunEnv: envRun,
})
Expand Down
7 changes: 2 additions & 5 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
package file

import (
ioutil "io/ioutil"
os "os"
filepath "path/filepath"

jsoniter "github.com/json-iterator/go"
)

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary

type WriteFileOptions struct {
PathFile string
Expand All @@ -24,7 +21,7 @@ func WriteFile(options *WriteFileOptions) error {
return err
}

err = ioutil.WriteFile(options.PathFile, options.ContentsBytes, 0644)
err = os.WriteFile(options.PathFile, options.ContentsBytes, 0644)
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/file/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@ package file

import (
io "io"
fs "io/fs"

gotoml "github.com/pelletier/go-toml"
)

func TOMLFileReaderCallbackToJSON(getFileCallback func() (fs.File, error)) ([]byte, error) {
file, err := getFileCallback()
if err != nil {
return nil, err
}
defer file.Close()

res, err := gotoml.LoadReader(file)
if err != nil {
return nil, err
}

return JSONGenericMapToBytes(res.ToMap())
}

func TOMLFileReaderToJSON(input fs.File, doClose bool) ([]byte, error) {
res, err := gotoml.LoadReader(input)
if err != nil {
return nil, err
}

if doClose {
defer input.Close()
}
return JSONGenericMapToBytes(res.ToMap())
}

func TOMLReaderToJSON(input io.Reader) ([]byte, error) {
res, err := gotoml.LoadReader(input)
if err != nil {
Expand Down

0 comments on commit 3fa1b70

Please sign in to comment.