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
|
// ConfigurationFormat is the common interface for all configuration parser
|
||||||
type ConfigurationFormat interface {
|
type ConfigurationFormat interface {
|
||||||
// Read the given file
|
// ReadFrom the given file
|
||||||
Read(io.Reader) error
|
// implements io.ReaderFrom
|
||||||
|
ReadFrom(io.Reader) (int64, error)
|
||||||
|
|
||||||
// Write the given update to the file
|
// WriteTo a file the updated content
|
||||||
Write(io.Writer) error
|
// implements io.WriterTo
|
||||||
|
WriteTo(io.Writer) (int64, error)
|
||||||
|
|
||||||
// Get the value of a field if it exists
|
// Get the value of a field if it exists
|
||||||
Get(string) (string, bool)
|
Get(string) (string, bool)
|
||||||
|
|
|
@ -13,26 +13,25 @@ type Ini struct {
|
||||||
parsed bool
|
parsed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read extract a reader as its JSON representation
|
// ReadFrom implements io.ReaderFrom
|
||||||
func (d *Ini) Read(_reader io.Reader) error {
|
func (d *Ini) ReadFrom(_reader io.Reader) (int64, error) {
|
||||||
|
|
||||||
// 1. get json decoder
|
// 1. get json decoder
|
||||||
file, err := ini.Load(ioutil.NopCloser(_reader))
|
file, err := ini.Load(ioutil.NopCloser(_reader))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.data = file
|
d.data = file
|
||||||
d.sections = file.SectionStrings()
|
d.sections = file.SectionStrings()
|
||||||
d.parsed = true
|
d.parsed = true
|
||||||
return nil
|
return 0, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the INI representation to a writer
|
// WriteTo implements io.WriterTo
|
||||||
func (d *Ini) Write(_writer io.Writer) error {
|
func (d *Ini) WriteTo(_writer io.Writer) (int64, error) {
|
||||||
_, err := d.data.WriteTo(_writer)
|
return d.data.WriteTo(_writer)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the value of a dot-separated path, and if it exists
|
// 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)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parse error: %s", err)
|
t.Errorf("parse error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
@ -62,7 +62,7 @@ func TestIniSetPathExists(t *testing.T) {
|
||||||
reader := bytes.NewBufferString(test.raw)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parse error: %s", err)
|
t.Errorf("parse error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
@ -110,7 +110,7 @@ func TestIniSetCreatePath(t *testing.T) {
|
||||||
reader := bytes.NewBufferString(test.raw)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("[%d] parse error: %s", i, err)
|
t.Errorf("[%d] parse error: %s", i, err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -11,25 +11,26 @@ type Json struct {
|
||||||
parsed bool
|
parsed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read extract a reader as its JSON representation
|
// ReadFrom implements io.ReaderFrom
|
||||||
func (d *Json) Read(_reader io.Reader) error {
|
func (d *Json) ReadFrom(_reader io.Reader) (int64, error) {
|
||||||
|
|
||||||
// 1. get json decoder
|
// 1. get json decoder
|
||||||
decoder := json.NewDecoder(_reader)
|
decoder := json.NewDecoder(_reader)
|
||||||
err := decoder.Decode(&d.data)
|
err := decoder.Decode(&d.data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.parsed = true
|
d.parsed = true
|
||||||
return nil
|
return 0, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the JSON representation to a writer
|
// WriteTo implements io.WriterTo
|
||||||
func (d *Json) Write(_writer io.Writer) error {
|
func (d *Json) WriteTo(_writer io.Writer) (int64, error) {
|
||||||
encoder := json.NewEncoder(_writer)
|
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)
|
// 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)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parse error: %s", err)
|
t.Errorf("parse error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
@ -63,7 +63,7 @@ func TestJsonGetNotString(t *testing.T) {
|
||||||
reader := bytes.NewBufferString(test.raw)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parse error: %s", err)
|
t.Errorf("parse error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
@ -99,7 +99,7 @@ func TestJsonSetPathExistsAndIsString(t *testing.T) {
|
||||||
reader := bytes.NewBufferString(test.raw)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parse error: %s", err)
|
t.Errorf("parse error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
@ -150,7 +150,7 @@ func TestJsonSetCreatePath(t *testing.T) {
|
||||||
reader := bytes.NewBufferString(test.raw)
|
reader := bytes.NewBufferString(test.raw)
|
||||||
|
|
||||||
// try to extract value
|
// try to extract value
|
||||||
err := parser.Read(reader)
|
_, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("[%d] parse error: %s", i, err)
|
t.Errorf("[%d] parse error: %s", i, err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -38,7 +38,7 @@ func Load(path string) (ConfigurationFormat, error) {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// parse
|
// parse
|
||||||
err = confFormat.Read(file)
|
_, err = confFormat.ReadFrom(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse file as '%s' | %s", extension, err)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err = format.Write(file); err != nil {
|
if _, err = format.WriteTo(file); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue