update cnf/common interface to implement io.ReaderFrom + io.WriterTo | tested ini+json file update
This commit is contained in:
parent
151ebce573
commit
cdd4b29334
|
@ -6,11 +6,13 @@ import (
|
|||
|
||||
// ConfigurationFormat is the common interface for all configuration parser
|
||||
type ConfigurationFormat interface {
|
||||
// Read the given file
|
||||
Read(io.Reader) error
|
||||
// ReadFrom the given file
|
||||
// implements io.ReaderFrom
|
||||
ReadFrom(io.Reader) (int64, error)
|
||||
|
||||
// Write the given update to the file
|
||||
Write(io.Writer) error
|
||||
// WriteTo a file the updated content
|
||||
// implements io.WriterTo
|
||||
WriteTo(io.Writer) (int64, error)
|
||||
|
||||
// Get the value of a field if it exists
|
||||
Get(string) (string, bool)
|
||||
|
|
|
@ -13,26 +13,25 @@ type Ini struct {
|
|||
parsed bool
|
||||
}
|
||||
|
||||
// Read extract a reader as its JSON representation
|
||||
func (d *Ini) Read(_reader io.Reader) error {
|
||||
// ReadFrom implements io.ReaderFrom
|
||||
func (d *Ini) ReadFrom(_reader io.Reader) (int64, error) {
|
||||
|
||||
// 1. get json decoder
|
||||
file, err := ini.Load(ioutil.NopCloser(_reader))
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
|
||||
d.data = file
|
||||
d.sections = file.SectionStrings()
|
||||
d.parsed = true
|
||||
return nil
|
||||
return 0, nil
|
||||
|
||||
}
|
||||
|
||||
// Write the INI representation to a writer
|
||||
func (d *Ini) Write(_writer io.Writer) error {
|
||||
_, err := d.data.WriteTo(_writer)
|
||||
return err
|
||||
// WriteTo implements io.WriterTo
|
||||
func (d *Ini) WriteTo(_writer io.Writer) (int64, error) {
|
||||
return d.data.WriteTo(_writer)
|
||||
}
|
||||
|
||||
// Get returns the value of a dot-separated path, and if it exists
|
||||
|
|
|
@ -22,7 +22,7 @@ func TestIniGet(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("parse error: %s", err)
|
||||
continue
|
||||
|
@ -62,7 +62,7 @@ func TestIniSetPathExists(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("parse error: %s", err)
|
||||
continue
|
||||
|
@ -110,7 +110,7 @@ func TestIniSetCreatePath(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
|
|
|
@ -11,25 +11,26 @@ type Json struct {
|
|||
parsed bool
|
||||
}
|
||||
|
||||
// Read extract a reader as its JSON representation
|
||||
func (d *Json) Read(_reader io.Reader) error {
|
||||
// ReadFrom implements io.ReaderFrom
|
||||
func (d *Json) ReadFrom(_reader io.Reader) (int64, error) {
|
||||
|
||||
// 1. get json decoder
|
||||
decoder := json.NewDecoder(_reader)
|
||||
err := decoder.Decode(&d.data)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
|
||||
d.parsed = true
|
||||
return nil
|
||||
return 0, nil
|
||||
|
||||
}
|
||||
|
||||
// Write the JSON representation to a writer
|
||||
func (d *Json) Write(_writer io.Writer) error {
|
||||
// WriteTo implements io.WriterTo
|
||||
func (d *Json) WriteTo(_writer io.Writer) (int64, error) {
|
||||
encoder := json.NewEncoder(_writer)
|
||||
return encoder.Encode(&d.data)
|
||||
encoder.SetIndent("", "\t")
|
||||
return 0, encoder.Encode(&d.data)
|
||||
}
|
||||
|
||||
// browse returns the target of a dot-separated path (as an interface{} chain where the last is the target if found)
|
||||
|
|
|
@ -23,7 +23,7 @@ func TestJsonGet(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("parse error: %s", err)
|
||||
continue
|
||||
|
@ -63,7 +63,7 @@ func TestJsonGetNotString(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("parse error: %s", err)
|
||||
continue
|
||||
|
@ -99,7 +99,7 @@ func TestJsonSetPathExistsAndIsString(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("parse error: %s", err)
|
||||
continue
|
||||
|
@ -150,7 +150,7 @@ func TestJsonSetCreatePath(t *testing.T) {
|
|||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
err := parser.Read(reader)
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
|
|
|
@ -38,7 +38,7 @@ func Load(path string) (ConfigurationFormat, error) {
|
|||
defer file.Close()
|
||||
|
||||
// parse
|
||||
err = confFormat.Read(file)
|
||||
_, err = confFormat.ReadFrom(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse file as '%s' | %s", extension, err)
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ func (d config) Exec(ctx ExecutionContext) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
if err = format.Write(file); err != nil {
|
||||
if _, err = format.WriteTo(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue