revise API: ParseXXX -> ReadXXXInto (ParseXXX functions that "just parse" may be added later)
diff --git a/gcfg.go b/gcfg.go
index 524cd43..b097163 100644
--- a/gcfg.go
+++ b/gcfg.go
@@ -2,7 +2,7 @@
// "name=value" pairs grouped into sections (gcfg files).
// Support for writing gcfg files may be added later.
//
-// See Parse and the examples to get an idea of how to use it.
+// See ReadInto and the examples to get an idea of how to use it.
//
// This package is still a work in progress, and both the supported syntax and
// the API is subject to change.
@@ -143,7 +143,7 @@
panic("never reached")
}
-// Parse reads gcfg formatted data from reader and sets the values into the
+// ReadInto reads gcfg formatted data from reader and sets the values into the
// corresponding fields in config.
//
// Config must be a pointer to a struct.
@@ -173,12 +173,12 @@
// For all other types, fmt.Sscanf is used to parse the value and set it to the
// field.
// This means that built-in Go types are parseable using the standard format,
-// and any user-defined types is parseable if it implements the fmt.Scanner
+// and any user-defined type is parseable if it implements the fmt.Scanner
// interface.
//
-// See ParseString for examples.
+// See ReadStringInto for examples.
//
-func Parse(config interface{}, reader io.Reader) error {
+func ReadInto(config interface{}, reader io.Reader) error {
r := bufio.NewReader(reader)
sect := (*string)(nil)
sectsub := ""
@@ -236,22 +236,24 @@
return nil
}
-// ParseString reads gcfg formatted data from str and sets the values into the
-// corresponding fields in config.
-// See Parse(config, reader) for the details of the parsing rules.
-func ParseString(config interface{}, str string) error {
+// ReadStringInto reads gcfg formatted data from str and sets the values into
+// the corresponding fields in config.
+// See ReadInto(config, reader) for a detailed description of how values are
+// parsed and set into config.
+func ReadStringInto(config interface{}, str string) error {
r := strings.NewReader(str)
- return Parse(config, r)
+ return ReadInto(config, r)
}
-// ParseFile reads gcfg formatted data from the file filename and sets the
+// ReadFileInto reads gcfg formatted data from the file filename and sets the
// values into the corresponding fields in config.
-// See Parse(config, reader) for the details of the parsing rules.
-func ParseFile(config interface{}, filename string) error {
+// See ReadInto(config, reader) for a detailed description of how values are
+// parsed and set into config.
+func ReadFileInto(config interface{}, filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
- return Parse(config, f)
+ return ReadInto(config, f)
}
diff --git a/gcfg_example_test.go b/gcfg_example_test.go
index ea680ca..7131b96 100644
--- a/gcfg_example_test.go
+++ b/gcfg_example_test.go
@@ -5,7 +5,7 @@
"log"
)
-func ExampleParseString() {
+func ExampleReadStringInto() {
cfgStr := `; Comment line
[section]
name=value # comment`
@@ -14,7 +14,7 @@
Name string
}
}{}
- err := ParseString(&cfg, cfgStr)
+ err := ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
@@ -22,7 +22,7 @@
// Output: value
}
-func ExampleParseString_bool() {
+func ExampleReadStringInto_bool() {
cfgStr := `; Comment line
[section]
switch=on`
@@ -31,7 +31,7 @@
Switch bool
}
}{}
- err := ParseString(&cfg, cfgStr)
+ err := ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
@@ -39,7 +39,7 @@
// Output: true
}
-func ExampleParseString_subsections() {
+func ExampleReadStringInto_subsections() {
cfgStr := `; Comment line
[profile "A"]
color = white
@@ -52,7 +52,7 @@
Color string
}
}{}
- err := ParseString(&cfg, cfgStr)
+ err := ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
diff --git a/gcfg_test.go b/gcfg_test.go
index 95e5c8a..76d0ccc 100644
--- a/gcfg_test.go
+++ b/gcfg_test.go
@@ -27,16 +27,16 @@
type sect04 struct{ Name string }
type conf04 struct{ Sub map[string]*sect04 }
-type parsetest struct {
+type readtest struct {
gcfg string
exp interface{}
ok bool
}
-var parsetests = []struct {
+var readtests = []struct {
group string
- tests []parsetest
-}{{"basic", []parsetest{
+ tests []readtest
+}{{"basic", []readtest{
// string value
{"[section]\nname=value", &conf01{sect01{"value"}}, true},
{"[section]\nname=", &conf01{sect01{""}}, true},
@@ -52,7 +52,7 @@
{"[section]\nname=\"value\"", &conf01{sect01{"value"}}, true},
{"[section]\nname=\" value \"", &conf01{sect01{" value "}}, true},
{"\n[section]\nname=\"value ; cmnt\"", &conf01{sect01{"value ; cmnt"}}, true},
-}}, {"bool", []parsetest{
+}}, {"bool", []readtest{
{"[section]\nbool=true", &conf02{sect02{true}}, true},
{"[section]\nbool=yes", &conf02{sect02{true}}, true},
{"[section]\nbool=on", &conf02{sect02{true}}, true},
@@ -64,7 +64,7 @@
{"[section]\nbool=t", &conf02{}, false},
{"[section]\nbool=truer", &conf02{}, false},
{"[section]\nbool=-1", &conf02{}, false},
-}}, {"whitespace", []parsetest{
+}}, {"whitespace", []readtest{
{" \n[section]\nbool=true", &conf02{sect02{true}}, true},
{" [section]\nbool=true", &conf02{sect02{true}}, true},
{"\t[section]\nbool=true", &conf02{sect02{true}}, true},
@@ -77,7 +77,7 @@
{"[section]\r\nbool=true", &conf02{sect02{true}}, true},
{"[section]\r\nbool=true\r\n", &conf02{sect02{true}}, true},
{";cmnt\r\n[section]\r\nbool=true\r\n", &conf02{sect02{true}}, true},
-}}, {"comments", []parsetest{
+}}, {"comments", []readtest{
{"; cmnt\n[section]\nname=value", &conf01{sect01{"value"}}, true},
{"# cmnt\n[section]\nname=value", &conf01{sect01{"value"}}, true},
{" ; cmnt\n[section]\nname=value", &conf01{sect01{"value"}}, true},
@@ -90,10 +90,10 @@
{"\n[section]\nname=value ; \"cmnt", &conf01{sect01{"value"}}, true},
{"\n[section]\nname=\"value ; cmnt\" ; cmnt", &conf01{sect01{"value ; cmnt"}}, true},
{"\n[section]\nname=; cmnt", &conf01{sect01{""}}, true},
-}}, {"subsections", []parsetest{
+}}, {"subsections", []readtest{
{"\n[sub \"A\"]\nname=value", &conf04{map[string]*sect04{"A": §04{"value"}}}, true},
{"\n[sub \"b\"]\nname=value", &conf04{map[string]*sect04{"b": §04{"value"}}}, true},
-}}, {"errors", []parsetest{
+}}, {"errors", []readtest{
// error: invalid line
{"\n[section]\n=", &conf01{}, false},
// error: line too long
@@ -108,15 +108,15 @@
}},
}
-func TestParse(t *testing.T) {
- for _, tg := range parsetests {
+func TestReadStringInto(t *testing.T) {
+ for _, tg := range readtests {
for i, tt := range tg.tests {
id := fmt.Sprintf("%s:%d", tg.group, i)
// get the type of the expected result
restyp := reflect.TypeOf(tt.exp).Elem()
// create a new instance to hold the actual result
res := reflect.New(restyp).Interface()
- err := ParseString(res, tt.gcfg)
+ err := ReadStringInto(res, tt.gcfg)
if tt.ok {
if err != nil {
t.Errorf("%s fail: got error %v, wanted ok", id, err)
@@ -141,9 +141,9 @@
}
}
-func TestParseFile(t *testing.T) {
+func TestReadFileInto(t *testing.T) {
res := &struct{ Section struct{ Name string } }{}
- err := ParseFile(res, "gcfg_test.gcfg")
+ err := ReadFileInto(res, "gcfg_test.gcfg")
if err != nil {
t.Fatal(err)
}