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

View File

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

View File

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

View File

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

View File

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