Skip to content

Commit 34de94e

Browse files
committed
fix issue #421
1 parent 88263a0 commit 34de94e

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

marshal.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
436436
if tree, ok := val.(*Tree); ok && mtypef.Anonymous && !opts.nameFromTag && !e.promoteAnon {
437437
e.appendTree(tval, tree)
438438
} else {
439+
val = e.wrapTomlValue(val, tval)
439440
tval.SetPathWithOptions([]string{opts.name}, SetOptions{
440441
Comment: opts.comment,
441442
Commented: opts.commented,
@@ -474,6 +475,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
474475
if err != nil {
475476
return nil, err
476477
}
478+
val = e.wrapTomlValue(val, tval)
477479
if e.quoteMapKeys {
478480
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
479481
if err != nil {
@@ -516,7 +518,6 @@ func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (int
516518

517519
// Convert given marshal value to toml value
518520
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
519-
e.line++
520521
if mtype.Kind() == reflect.Ptr {
521522
switch {
522523
case isCustomMarshaler(mtype):
@@ -579,6 +580,25 @@ func (e *Encoder) appendTree(t, o *Tree) error {
579580
return nil
580581
}
581582

583+
// Create a toml value with the current line number as the position line
584+
func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
585+
_, isTree := val.(*Tree)
586+
_, isTreeS := val.([]*Tree)
587+
if isTree || isTreeS {
588+
return val
589+
}
590+
591+
ret := &tomlValue{
592+
value: val,
593+
position: Position{
594+
e.line,
595+
parent.position.Col,
596+
},
597+
}
598+
e.line++
599+
return ret
600+
}
601+
582602
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
583603
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
584604
// sub-structs, and only definite types can be unmarshaled.

marshal_test.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,69 @@ import (
1414
)
1515

1616
type basicMarshalTestStruct struct {
17-
String string `toml:"Zstring"`
18-
StringList []string `toml:"Ystrlist"`
19-
Sub basicMarshalTestSubStruct `toml:"Xsubdoc"`
20-
SubList []basicMarshalTestSubStruct `toml:"Wsublist"`
17+
String string `toml:"Zstring"`
18+
StringList []string `toml:"Ystrlist"`
19+
BasicMarshalTestSubAnonymousStruct
20+
Sub basicMarshalTestSubStruct `toml:"Xsubdoc"`
21+
SubList []basicMarshalTestSubStruct `toml:"Wsublist"`
2122
}
2223

2324
type basicMarshalTestSubStruct struct {
2425
String2 string
2526
}
2627

28+
type BasicMarshalTestSubAnonymousStruct struct {
29+
String3 string
30+
}
31+
2732
var basicTestData = basicMarshalTestStruct{
28-
String: "Hello",
29-
StringList: []string{"Howdy", "Hey There"},
30-
Sub: basicMarshalTestSubStruct{"One"},
31-
SubList: []basicMarshalTestSubStruct{{"Two"}, {"Three"}},
33+
String: "Hello",
34+
StringList: []string{"Howdy", "Hey There"},
35+
BasicMarshalTestSubAnonymousStruct: BasicMarshalTestSubAnonymousStruct{"One"},
36+
Sub: basicMarshalTestSubStruct{"Two"},
37+
SubList: []basicMarshalTestSubStruct{{"Three"}, {"Four"}},
3238
}
3339

34-
var basicTestToml = []byte(`Ystrlist = ["Howdy", "Hey There"]
40+
var basicTestToml = []byte(`String3 = "One"
41+
Ystrlist = ["Howdy", "Hey There"]
3542
Zstring = "Hello"
3643
3744
[[Wsublist]]
38-
String2 = "Two"
45+
String2 = "Three"
3946
4047
[[Wsublist]]
41-
String2 = "Three"
48+
String2 = "Four"
4249
4350
[Xsubdoc]
44-
String2 = "One"
51+
String2 = "Two"
4552
`)
4653

47-
var basicTestTomlCustomIndentation = []byte(`Ystrlist = ["Howdy", "Hey There"]
54+
var basicTestTomlCustomIndentation = []byte(`String3 = "One"
55+
Ystrlist = ["Howdy", "Hey There"]
4856
Zstring = "Hello"
4957
5058
[[Wsublist]]
51-
String2 = "Two"
59+
String2 = "Three"
5260
5361
[[Wsublist]]
54-
String2 = "Three"
62+
String2 = "Four"
5563
5664
[Xsubdoc]
57-
String2 = "One"
65+
String2 = "Two"
5866
`)
5967

6068
var basicTestTomlOrdered = []byte(`Zstring = "Hello"
6169
Ystrlist = ["Howdy", "Hey There"]
70+
String3 = "One"
6271
6372
[Xsubdoc]
64-
String2 = "One"
65-
66-
[[Wsublist]]
6773
String2 = "Two"
6874
6975
[[Wsublist]]
7076
String2 = "Three"
77+
78+
[[Wsublist]]
79+
String2 = "Four"
7180
`)
7281

7382
var marshalTestToml = []byte(`title = "TOML Marshal Testing"

toml.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ func (t *Tree) SetPathWithOptions(keys []string, opts SetOptions, value interfac
315315
toInsert = value
316316
case *tomlValue:
317317
v.comment = opts.Comment
318+
v.commented = opts.Commented
319+
v.multiline = opts.Multiline
318320
toInsert = v
319321
default:
320322
toInsert = &tomlValue{value: value,

0 commit comments

Comments
 (0)