8000 support optional section + key/value map as section by mandelsoft · Pull Request #18 · go-gcfg/gcfg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

support optional section + key/value map as section #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,51 @@ variable-name=value # comment`

func ExampleReadStringInto_tags() {
cfgStr := `; Comment line
[section]
var-name=value # comment`
[tags]
tag1=value1 # a tag
tag2=value2 # a second tag`
cfg := struct {
Section struct {
FieldName string `gcfg:"var-name"`
Tags map[string]string
}{}
err := gcfg.ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
for k, v := range cfg.Tags {
fmt.Printf("%s = %s\n", k, v)
}
// Output: tag1 = value1
// tag2 = value2
}

func ExampleReadStringInto_optional_section() {
cfgStr := `; Comment line
[Section]
Name=value`
cfg := struct {
Optional *struct {
Name string
}
Section *struct {
Name string
}
}{}
err := gcfg.ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
fmt.Println(cfg.Section.FieldName)
// Output: value
if cfg.Optional == nil {
fmt.Println("optional not given")
} else {
fmt.Printf("Optional.Name=%s\n", cfg.Optional.Name)
}
if cfg.Section == nil {
fmt.Println("section not given")
} else {
fmt.Printf("Section.Name = %s\n", cfg.Section.Name)
}
// Output: optional not given
// Section.Name = value
}

func ExampleReadStringInto_subsections() {
Expand Down
8 changes: 4 additions & 4 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ func readInto(config interface{}, fset *token.FileSet, file *token.File,
if err != nil {
return err
}
err = readIntoPass(c, config, fset, file, src, true)
if err != nil {
return err
}
//err = readIntoPass(c, config, fset, file, src, true)
//if err != nil {
// return err
//}
return c.Done()
}

Expand Down
140 changes: 139 additions & 1 deletion read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,152 @@ func TestReadStringIntoExtraData(t *testing.T) {
}
}

func TestOptionalPointerSection(t *testing.T) {
res := &struct {
Section *struct {
Name string
}
NonOptional struct {
Name string
}
}{}
cfg := `
[NonOptional]`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if res.Section != nil {
t.Errorf("unexpection section Section ")
}
}

func TestPointerSection(t *testing.T) {
res := &struct {
Section *struct {
Name string
}
}{}
cfg := `
[section]
name = value`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if res.Section == nil {
t.Errorf("section Section not read")
}
if res.Section.Name != "value" {
t.Errorf("res.Section.Name=%q; want %q", res.Section.Name, "value")
}
}

func TestPointerSubSection(t *testing.T) {
res := &struct {
Section map[string]*struct {
Name string
}
}{}
cfg := `
[Section "test"]
name=value`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if len(res.Section) != 1 {
t.Errorf("len(res.Section)=%d; want %d", len(res.Section), 1)
}
if res.Section["test"] == nil {
t.Errorf("res.Section[\"test\"] is nil")
}
if res.Section["test"].Name != "value" {
t.Errorf("res.Section[\"test\"].Name=%q; want %q", res.Section["test"].Name, "value")
}
}

func TestNonPointerSubSection(t *testing.T) {
res := &struct {
Section map[string]struct {
Name string
}
}{}
cfg := `
[Section "test"]
name=value`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if len(res.Section) != 1 {
t.Errorf("len(res.Section)=%d; want %d", len(res.Section), 1)
}
if _, ok := res.Section["test"]; !ok {
t.Errorf("res.Section[\"test\"] is not set")
}
if res.Section["test"].Name != "value" {
t.Errorf("res.Section[\"test\"].Name=%q; want %q", res.Section["test"].Name, "value")
}
}

func TestReadKeyValueMap(t *testing.T) {
res := &struct {
Section map[string]string
}{}
cfg := `
[section]
name = value
name2 = value2`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if res.Section["name"] != "value" {
t.Errorf("res.Section[\"name\"]=%q; want %q", res.Section["name"], "value")
}
if res.Section["name2"] != "value2" {
t.Errorf("res.Section[\"name2\"]=%q; want %q", res.Section["name2"], "value2")
}
if len(res.Section) != 2 {
t.Errorf("len(res.Section)=%d; want %d", len(res.Section), 2)
}
}

func TestReadKeyValueMapAsSubSection(t *testing.T) {
res := &struct {
Section map[string]map[string]string
}{}
cfg := `
[section "test"]
name = value
name2 = value2`
err := ReadStringInto(res, cfg)
if err != nil {
t.Error(err)
}
if len(res.Section) != 1 {
t.Errorf("len(res.Section)=%d; want %d", len(res.Section), 1)
}
if res.Section["test"]["name"] != "value" {
t.Errorf("res.Section[\"test\"][\"name\"]=%q; want %q", res.Section["test"]["name"], "value")
}
if res.Section["test"]["name2"] != "value2" {
t.Errorf("res.Section[\"test\"][\"name2\"]=%q; want %q", res.Section["test"]["name2"], "value2")
}
if len(res.Section["test"]) != 2 {
t.Errorf("len(res.Section[\"test\"])=%d; want %d", len(res.Section), 2)
}
}

var panictests = []struct {
id string
config interface{}
gcfg string
}{
{"top", struct{}{}, "[section]\nname=value"},
{"section", &struct{ Section string }{}, "[section]\nname=value"},
{"subsection", &struct{ Section map[string]string }{}, "[section \"subsection\"]\nname=value"},
{"subsection", &struct{ Section map[string]**struct{} }{}, "[section \"subsection\"]\nname=value"},
}

func testPanic(t *testing.T, id string, config interface{}, gcfg string) {
Expand Down
Loading
0