fix issue #421
This commit is contained in:
+21
-1
@@ -436,6 +436,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
if tree, ok := val.(*Tree); ok && mtypef.Anonymous && !opts.nameFromTag && !e.promoteAnon {
|
if tree, ok := val.(*Tree); ok && mtypef.Anonymous && !opts.nameFromTag && !e.promoteAnon {
|
||||||
e.appendTree(tval, tree)
|
e.appendTree(tval, tree)
|
||||||
} else {
|
} else {
|
||||||
|
val = e.wrapTomlValue(val, tval)
|
||||||
tval.SetPathWithOptions([]string{opts.name}, SetOptions{
|
tval.SetPathWithOptions([]string{opts.name}, SetOptions{
|
||||||
Comment: opts.comment,
|
Comment: opts.comment,
|
||||||
Commented: opts.commented,
|
Commented: opts.commented,
|
||||||
@@ -474,6 +475,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
val = e.wrapTomlValue(val, tval)
|
||||||
if e.quoteMapKeys {
|
if e.quoteMapKeys {
|
||||||
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
|
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -516,7 +518,6 @@ func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (int
|
|||||||
|
|
||||||
// Convert given marshal value to toml value
|
// Convert given marshal value to toml value
|
||||||
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
|
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
|
||||||
e.line++
|
|
||||||
if mtype.Kind() == reflect.Ptr {
|
if mtype.Kind() == reflect.Ptr {
|
||||||
switch {
|
switch {
|
||||||
case isCustomMarshaler(mtype):
|
case isCustomMarshaler(mtype):
|
||||||
@@ -579,6 +580,25 @@ func (e *Encoder) appendTree(t, o *Tree) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a toml value with the current line number as the position line
|
||||||
|
func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
|
||||||
|
_, isTree := val.(*Tree)
|
||||||
|
_, isTreeS := val.([]*Tree)
|
||||||
|
if isTree || isTreeS {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &tomlValue{
|
||||||
|
value: val,
|
||||||
|
position: Position{
|
||||||
|
e.line,
|
||||||
|
parent.position.Col,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
e.line++
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
|
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
|
||||||
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
|
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
|
||||||
// sub-structs, and only definite types can be unmarshaled.
|
// sub-structs, and only definite types can be unmarshaled.
|
||||||
|
|||||||
+31
-22
@@ -14,60 +14,69 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type basicMarshalTestStruct struct {
|
type basicMarshalTestStruct struct {
|
||||||
String string `toml:"Zstring"`
|
String string `toml:"Zstring"`
|
||||||
StringList []string `toml:"Ystrlist"`
|
StringList []string `toml:"Ystrlist"`
|
||||||
Sub basicMarshalTestSubStruct `toml:"Xsubdoc"`
|
BasicMarshalTestSubAnonymousStruct
|
||||||
SubList []basicMarshalTestSubStruct `toml:"Wsublist"`
|
Sub basicMarshalTestSubStruct `toml:"Xsubdoc"`
|
||||||
|
SubList []basicMarshalTestSubStruct `toml:"Wsublist"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicMarshalTestSubStruct struct {
|
type basicMarshalTestSubStruct struct {
|
||||||
String2 string
|
String2 string
|
||||||
}
|
}
|
||||||
|
|
||||||
var basicTestData = basicMarshalTestStruct{
|
type BasicMarshalTestSubAnonymousStruct struct {
|
||||||
String: "Hello",
|
String3 string
|
||||||
StringList: []string{"Howdy", "Hey There"},
|
|
||||||
Sub: basicMarshalTestSubStruct{"One"},
|
|
||||||
SubList: []basicMarshalTestSubStruct{{"Two"}, {"Three"}},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var basicTestToml = []byte(`Ystrlist = ["Howdy", "Hey There"]
|
var basicTestData = basicMarshalTestStruct{
|
||||||
Zstring = "Hello"
|
String: "Hello",
|
||||||
|
StringList: []string{"Howdy", "Hey There"},
|
||||||
|
BasicMarshalTestSubAnonymousStruct: BasicMarshalTestSubAnonymousStruct{"One"},
|
||||||
|
Sub: basicMarshalTestSubStruct{"Two"},
|
||||||
|
SubList: []basicMarshalTestSubStruct{{"Three"}, {"Four"}},
|
||||||
|
}
|
||||||
|
|
||||||
[[Wsublist]]
|
var basicTestToml = []byte(`String3 = "One"
|
||||||
String2 = "Two"
|
Ystrlist = ["Howdy", "Hey There"]
|
||||||
|
Zstring = "Hello"
|
||||||
|
|
||||||
[[Wsublist]]
|
[[Wsublist]]
|
||||||
String2 = "Three"
|
String2 = "Three"
|
||||||
|
|
||||||
|
[[Wsublist]]
|
||||||
|
String2 = "Four"
|
||||||
|
|
||||||
[Xsubdoc]
|
[Xsubdoc]
|
||||||
String2 = "One"
|
String2 = "Two"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
var basicTestTomlCustomIndentation = []byte(`Ystrlist = ["Howdy", "Hey There"]
|
var basicTestTomlCustomIndentation = []byte(`String3 = "One"
|
||||||
|
Ystrlist = ["Howdy", "Hey There"]
|
||||||
Zstring = "Hello"
|
Zstring = "Hello"
|
||||||
|
|
||||||
[[Wsublist]]
|
|
||||||
String2 = "Two"
|
|
||||||
|
|
||||||
[[Wsublist]]
|
[[Wsublist]]
|
||||||
String2 = "Three"
|
String2 = "Three"
|
||||||
|
|
||||||
|
[[Wsublist]]
|
||||||
|
String2 = "Four"
|
||||||
|
|
||||||
[Xsubdoc]
|
[Xsubdoc]
|
||||||
String2 = "One"
|
String2 = "Two"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
var basicTestTomlOrdered = []byte(`Zstring = "Hello"
|
var basicTestTomlOrdered = []byte(`Zstring = "Hello"
|
||||||
Ystrlist = ["Howdy", "Hey There"]
|
Ystrlist = ["Howdy", "Hey There"]
|
||||||
|
String3 = "One"
|
||||||
|
|
||||||
[Xsubdoc]
|
[Xsubdoc]
|
||||||
String2 = "One"
|
|
||||||
|
|
||||||
[[Wsublist]]
|
|
||||||
String2 = "Two"
|
String2 = "Two"
|
||||||
|
|
||||||
[[Wsublist]]
|
[[Wsublist]]
|
||||||
String2 = "Three"
|
String2 = "Three"
|
||||||
|
|
||||||
|
[[Wsublist]]
|
||||||
|
String2 = "Four"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
var marshalTestToml = []byte(`title = "TOML Marshal Testing"
|
var marshalTestToml = []byte(`title = "TOML Marshal Testing"
|
||||||
|
|||||||
@@ -315,6 +315,8 @@ func (t *Tree) SetPathWithOptions(keys []string, opts SetOptions, value interfac
|
|||||||
toInsert = value
|
toInsert = value
|
||||||
case *tomlValue:
|
case *tomlValue:
|
||||||
v.comment = opts.Comment
|
v.comment = opts.Comment
|
||||||
|
v.commented = opts.Commented
|
||||||
|
v.multiline = opts.Multiline
|
||||||
toInsert = v
|
toInsert = v
|
||||||
default:
|
default:
|
||||||
toInsert = &tomlValue{value: value,
|
toInsert = &tomlValue{value: value,
|
||||||
|
|||||||
Reference in New Issue
Block a user