unexport cnf/Json to json and cnf/Ini to ini

This commit is contained in:
xdrm-brackets 2018-11-11 15:34:00 +01:00
parent f094254acc
commit 40d6526b64
5 changed files with 26 additions and 26 deletions

View File

@ -1,23 +1,23 @@
package cnf
import (
"github.com/go-ini/ini"
lib "github.com/go-ini/ini"
"io"
"io/ioutil"
"strings"
)
type Ini struct {
type ini struct {
sections []string
data *ini.File
data *lib.File
parsed bool
}
// ReadFrom implements io.ReaderFrom
func (d *Ini) ReadFrom(_reader io.Reader) (int64, error) {
func (d *ini) ReadFrom(_reader io.Reader) (int64, error) {
// 1. get json decoder
file, err := ini.Load(ioutil.NopCloser(_reader))
file, err := lib.Load(ioutil.NopCloser(_reader))
if err != nil {
return 0, err
}
@ -30,13 +30,13 @@ func (d *Ini) ReadFrom(_reader io.Reader) (int64, error) {
}
// WriteTo implements io.WriterTo
func (d *Ini) WriteTo(_writer io.Writer) (int64, error) {
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
// the maximum depth is 2 : Section.Field
func (d *Ini) Get(_path string) (string, bool) {
func (d *ini) Get(_path string) (string, bool) {
// 1. split path
path := strings.Split(_path, ".")
@ -80,7 +80,7 @@ func (d *Ini) Get(_path string) (string, bool) {
}
// Set the value of a dot-separated path, and creates it if not found
func (d *Ini) Set(_path, _value string) bool {
func (d *ini) Set(_path, _value string) bool {
// 1. split path
path := strings.Split(_path, ".")

View File

@ -18,7 +18,7 @@ func TestIniGet(t *testing.T) {
for _, test := range tests {
parser := new(Ini)
parser := new(ini)
reader := bytes.NewBufferString(test.raw)
// try to extract value
@ -58,7 +58,7 @@ func TestIniSetPathExists(t *testing.T) {
for _, test := range tests {
parser := new(Ini)
parser := new(ini)
reader := bytes.NewBufferString(test.raw)
// try to extract value
@ -106,7 +106,7 @@ func TestIniSetCreatePath(t *testing.T) {
for i, test := range tests {
parser := new(Ini)
parser := new(ini)
reader := bytes.NewBufferString(test.raw)
// try to extract value

View File

@ -1,21 +1,21 @@
package cnf
import (
"encoding/json"
lib "encoding/json"
"io"
"strings"
)
type Json struct {
type json struct {
data interface{}
parsed bool
}
// ReadFrom implements io.ReaderFrom
func (d *Json) ReadFrom(_reader io.Reader) (int64, error) {
func (d *json) ReadFrom(_reader io.Reader) (int64, error) {
// 1. get json decoder
decoder := json.NewDecoder(_reader)
decoder := lib.NewDecoder(_reader)
err := decoder.Decode(&d.data)
if err != nil {
return 0, err
@ -27,14 +27,14 @@ func (d *Json) ReadFrom(_reader io.Reader) (int64, error) {
}
// WriteTo implements io.WriterTo
func (d *Json) WriteTo(_writer io.Writer) (int64, error) {
encoder := json.NewEncoder(_writer)
func (d *json) WriteTo(_writer io.Writer) (int64, error) {
encoder := lib.NewEncoder(_writer)
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)
func (d *Json) browse(_path string) ([]interface{}, bool) {
func (d *json) browse(_path string) ([]interface{}, bool) {
// 1. extract path
path := strings.Split(_path, ".")
@ -64,7 +64,7 @@ func (d *Json) browse(_path string) ([]interface{}, bool) {
}
// Get returns the value of a dot-separated path, and if it exists
func (d *Json) Get(_path string) (string, bool) {
func (d *json) Get(_path string) (string, bool) {
// 1. browse path
chain, found := d.browse(_path)
@ -79,7 +79,7 @@ func (d *Json) Get(_path string) (string, bool) {
}
// Set the value of a dot-separated path, and creates it if not found
func (d *Json) Set(_path, _value string) bool {
func (d *json) Set(_path, _value string) bool {
// 1. browse path + create it if does not exist
path := strings.Split(_path, ".")

View File

@ -19,7 +19,7 @@ func TestJsonGet(t *testing.T) {
for _, test := range tests {
parser := new(Json)
parser := new(json)
reader := bytes.NewBufferString(test.raw)
// try to extract value
@ -59,7 +59,7 @@ func TestJsonGetNotString(t *testing.T) {
for _, test := range tests {
parser := new(Json)
parser := new(json)
reader := bytes.NewBufferString(test.raw)
// try to extract value
@ -95,7 +95,7 @@ func TestJsonSetPathExistsAndIsString(t *testing.T) {
for _, test := range tests {
parser := new(Json)
parser := new(json)
reader := bytes.NewBufferString(test.raw)
// try to extract value
@ -146,7 +146,7 @@ func TestJsonSetCreatePath(t *testing.T) {
for i, test := range tests {
parser := new(Json)
parser := new(json)
reader := bytes.NewBufferString(test.raw)
// try to extract value

View File

@ -68,9 +68,9 @@ func loadFromExtension(ext string) ConfigurationFormat {
// select configuration or fail if not known
switch ext {
case ".json":
return new(Json)
return new(json)
case ".ini":
return new(Ini)
return new(ini)
default:
return nil
}