Skip to content
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

Add ability to encode comments with desc tag #193

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,21 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value) {
continue
}

enc.addComment(key.add(keyName), sft.Tag)
enc.encode(key.add(keyName), sf)
}
}
writeFields(fieldsDirect)
writeFields(fieldsSub)
}

func (enc *Encoder) addComment(key Key, tag reflect.StructTag) {
if s := tag.Get("desc"); s != "" {
enc.wf("%s#%s\n", enc.indentStr(key), s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a space after the #

enc.hasWritten = false
}
}

// tomlTypeName returns the TOML type name of the Go value's type. It is
// used to determine whether the types of array elements are mixed (which is
// forbidden). If the Go value is nil, then it is illegal for it to be an array
Expand Down
42 changes: 42 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,48 @@ ArrayOfMixedSlices = [[1, 2], ["a", "b"]]
},
wantError: errAnything,
},
"simple fields with comments": {
input: struct {
Int1 int `desc:"an integer"`
Float float64 `desc:"a float"`
Bool bool `desc:"a bool"`
}{0, 0.0, false},
wantOutput: "#an integer\nInt1 = 0\n#a float\nFloat = 0.0\n#a bool\nBool = false\n",
},
"embedded struct with comments": {
input: struct {
Parent struct {
Name string `desc:"my name"`
Value int `desc:"my value"`
} `desc:"parent"`
}{struct {
Name string `desc:"my name"`
Value int `desc:"my value"`
}{"hello", 10}},
wantOutput: "#parent\n[Parent]\n #my name\n Name = \"hello\"\n #my value\n Value = 10\n",
},
"slice with comments": {
input: struct {
Primes []int `desc:"prime numbers"`
}{[]int{1, 2, 3, 5, 7}},
wantOutput: "#prime numbers\nPrimes = [1, 2, 3, 5, 7]\n",
},
"map with comments": {
input: struct {
Library map[string]interface{} `desc:"a library"`
}{map[string]interface{}{"name":"hello", "age": 99}},
wantOutput: "#a library\n[Library]\n age = 99\n name = \"hello\"\n",
},
"2 layer embedded struct with comments": {
input: struct {
Parent struct {
Child struct {
Int int `desc:"child int"`
} `desc:"child struct"`
} `desc:"parent struct"`
} {},
wantOutput: "#parent struct\n[Parent]\n #child struct\n [Parent.Child]\n #child int\n Int = 0\n",
},
}
for label, test := range tests {
encodeExpected(t, label, test.input, test.wantOutput, test.wantError)
Expand Down