Skip to content

Commit acdc450

Browse files
authored
Fix backward incompatibility for Set* methods (#213)
Patch #185 introduced a backward incompatibility by changing the arguments of the `Set*` methods on `Tree`. This change restores the arguments to what they previous were, and introduces `SetWithComment` and `SetPathWithComment` to perform the same action.
1 parent 778c285 commit acdc450

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

marshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
230230
if err != nil {
231231
return nil, err
232232
}
233-
tval.Set(opts.name, opts.comment, opts.commented, val)
233+
tval.SetWithComment(opts.name, opts.comment, opts.commented, val)
234234
}
235235
}
236236
case reflect.Map:
@@ -245,9 +245,9 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
245245
if err != nil {
246246
return nil, err
247247
}
248-
tval.SetPath([]string{keyStr}, "", false, val)
248+
tval.SetPath([]string{keyStr}, val)
249249
} else {
250-
tval.Set(key.String(), "", false, val)
250+
tval.Set(key.String(), val)
251251
}
252252
}
253253
}

parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (p *tomlParser) parseGroupArray() tomlParserStateFn {
111111
newTree := newTree()
112112
newTree.position = startToken.Position
113113
array = append(array, newTree)
114-
p.tree.SetPath(p.currentTable, "", false, array)
114+
p.tree.SetPath(p.currentTable, array)
115115

116116
// remove all keys that were children of this table array
117117
prefix := key.val + "."
@@ -345,7 +345,7 @@ Loop:
345345
key := p.getToken()
346346
p.assume(tokenEqual)
347347
value := p.parseRvalue()
348-
tree.Set(key.val, "", false, value)
348+
tree.Set(key.val, value)
349349
case tokenComma:
350350
if previous == nil {
351351
p.raiseError(follow, "inline table cannot start with a comma")

parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func assertTree(t *testing.T, tree *Tree, err error, ref map[string]interface{})
4747
func TestCreateSubTree(t *testing.T) {
4848
tree := newTree()
4949
tree.createSubTree([]string{"a", "b", "c"}, Position{})
50-
tree.Set("a.b.c", "", false, 42)
50+
tree.Set("a.b.c", 42)
5151
if tree.Get("a.b.c") != 42 {
5252
t.Fail()
5353
}

toml.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,26 @@ func (t *Tree) GetDefault(key string, def interface{}) interface{} {
178178
// Set an element in the tree.
179179
// Key is a dot-separated path (e.g. a.b.c).
180180
// Creates all necessary intermediate trees, if needed.
181-
func (t *Tree) Set(key string, comment string, commented bool, value interface{}) {
182-
t.SetPath(strings.Split(key, "."), comment, commented, value)
181+
func (t *Tree) Set(key string, value interface{}) {
182+
t.SetWithComment(key, "", false, value)
183+
}
184+
185+
// SetWithComment is the same as Set, but allows you to provide comment
186+
// information to the key, that will be reused by Marshal().
187+
func (t *Tree) SetWithComment(key string, comment string, commented bool, value interface{}) {
188+
t.SetPathWithComment(strings.Split(key, "."), comment, commented, value)
183189
}
184190

185191
// SetPath sets an element in the tree.
186192
// Keys is an array of path elements (e.g. {"a","b","c"}).
187193
// Creates all necessary intermediate trees, if needed.
188-
func (t *Tree) SetPath(keys []string, comment string, commented bool, value interface{}) {
194+
func (t *Tree) SetPath(keys []string, value interface{}) {
195+
t.SetPathWithComment(keys, "", false, value)
196+
}
197+
198+
// SetPathWithComment is the same as SetPath, but allows you to provide comment
199+
// information to the key, that will be reused by Marshal().
200+
func (t *Tree) SetPathWithComment(keys []string, comment string, commented bool, value interface{}) {
189201
subtree := t
190202
for _, intermediateKey := range keys[:len(keys)-1] {
191203
nextTree, exists := subtree.values[intermediateKey]

0 commit comments

Comments
 (0)