Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8f6d0d8be7 | |||
| 3c4b709fed | |||
| adacebd8c7 | |||
| 8bbb673431 | |||
| 2377ac4bc0 |
@@ -1,3 +1,4 @@
|
|||||||
* text=auto
|
* text=auto
|
||||||
|
|
||||||
benchmark/benchmark.toml text eol=lf
|
benchmark/benchmark.toml text eol=lf
|
||||||
|
testdata/** text eol=lf
|
||||||
|
|||||||
@@ -161,11 +161,11 @@ Execution time speedup compared to other Go TOML libraries:
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr><td>Marshal/HugoFrontMatter-2</td><td>1.9x</td><td>1.9x</td></tr>
|
<tr><td>Marshal/HugoFrontMatter-2</td><td>1.9x</td><td>1.9x</td></tr>
|
||||||
<tr><td>Marshal/ReferenceFile/map-2</td><td>1.7x</td><td>1.9x</td></tr>
|
<tr><td>Marshal/ReferenceFile/map-2</td><td>1.7x</td><td>1.8x</td></tr>
|
||||||
<tr><td>Marshal/ReferenceFile/struct-2</td><td>2.4x</td><td>2.6x</td></tr>
|
<tr><td>Marshal/ReferenceFile/struct-2</td><td>2.2x</td><td>2.5x</td></tr>
|
||||||
<tr><td>Unmarshal/HugoFrontMatter-2</td><td>2.9x</td><td>2.5x</td></tr>
|
<tr><td>Unmarshal/HugoFrontMatter-2</td><td>2.9x</td><td>2.9x</td></tr>
|
||||||
<tr><td>Unmarshal/ReferenceFile/map-2</td><td>2.7x</td><td>2.6x</td></tr>
|
<tr><td>Unmarshal/ReferenceFile/map-2</td><td>2.6x</td><td>2.9x</td></tr>
|
||||||
<tr><td>Unmarshal/ReferenceFile/struct-2</td><td>4.8x</td><td>5.1x</td></tr>
|
<tr><td>Unmarshal/ReferenceFile/struct-2</td><td>4.4x</td><td>5.3x</td></tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<details><summary>See more</summary>
|
<details><summary>See more</summary>
|
||||||
@@ -178,17 +178,17 @@ provided for completeness.</p>
|
|||||||
<tr><th>Benchmark</th><th>go-toml v1</th><th>BurntSushi/toml</th></tr>
|
<tr><th>Benchmark</th><th>go-toml v1</th><th>BurntSushi/toml</th></tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr><td>Marshal/SimpleDocument/map-2</td><td>1.7x</td><td>2.1x</td></tr>
|
<tr><td>Marshal/SimpleDocument/map-2</td><td>1.8x</td><td>2.9x</td></tr>
|
||||||
<tr><td>Marshal/SimpleDocument/struct-2</td><td>2.5x</td><td>2.8x</td></tr>
|
<tr><td>Marshal/SimpleDocument/struct-2</td><td>2.7x</td><td>4.2x</td></tr>
|
||||||
<tr><td>Unmarshal/SimpleDocument/map-2</td><td>4.1x</td><td>3.1x</td></tr>
|
<tr><td>Unmarshal/SimpleDocument/map-2</td><td>4.5x</td><td>3.1x</td></tr>
|
||||||
<tr><td>Unmarshal/SimpleDocument/struct-2</td><td>6.4x</td><td>4.3x</td></tr>
|
<tr><td>Unmarshal/SimpleDocument/struct-2</td><td>6.2x</td><td>3.9x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/example-2</td><td>3.4x</td><td>3.2x</td></tr>
|
<tr><td>UnmarshalDataset/example-2</td><td>3.1x</td><td>3.5x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/code-2</td><td>2.2x</td><td>2.5x</td></tr>
|
<tr><td>UnmarshalDataset/code-2</td><td>2.3x</td><td>3.1x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/twitter-2</td><td>2.8x</td><td>2.7x</td></tr>
|
<tr><td>UnmarshalDataset/twitter-2</td><td>2.5x</td><td>2.6x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/citm_catalog-2</td><td>2.2x</td><td>2.0x</td></tr>
|
<tr><td>UnmarshalDataset/citm_catalog-2</td><td>2.1x</td><td>2.2x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/canada-2</td><td>1.8x</td><td>1.4x</td></tr>
|
<tr><td>UnmarshalDataset/canada-2</td><td>1.6x</td><td>1.3x</td></tr>
|
||||||
<tr><td>UnmarshalDataset/config-2</td><td>4.4x</td><td>2.9x</td></tr>
|
<tr><td>UnmarshalDataset/config-2</td><td>4.3x</td><td>3.2x</td></tr>
|
||||||
<tr><td>[Geo mean]</td><td>2.8x</td><td>2.6x</td></tr>
|
<tr><td>[Geo mean]</td><td>2.7x</td><td>2.8x</td></tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<p>This table can be generated with <code>./ci.sh benchmark -a -html</code>.</p>
|
<p>This table can be generated with <code>./ci.sh benchmark -a -html</code>.</p>
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ cover() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
pushd "$dir"
|
pushd "$dir"
|
||||||
go test -covermode=atomic -coverprofile=coverage.out ./...
|
go test -covermode=atomic -coverpkg=./... -coverprofile=coverage.out.tmp ./...
|
||||||
|
cat coverage.out.tmp | grep -v testsuite | grep -v tomltestgen | grep -v gotoml-test-decoder > coverage.out
|
||||||
go tool cover -func=coverage.out
|
go tool cover -func=coverage.out
|
||||||
popd
|
popd
|
||||||
|
|
||||||
@@ -103,16 +104,23 @@ coverage() {
|
|||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
target_pct="$(cat ${target_out} |sed -E 's/.*total.*\t([0-9.]+)%/\1/;t;d')"
|
target_pct="$(tail -n2 ${target_out} | head -n1 | sed -E 's/.*total.*\t([0-9.]+)%.*/\1/')"
|
||||||
head_pct="$(cat ${head_out} |sed -E 's/.*total.*\t([0-9.]+)%/\1/;t;d')"
|
head_pct="$(tail -n2 ${head_out} | head -n1 | sed -E 's/.*total.*\t([0-9.]+)%/\1/')"
|
||||||
echo "Results: ${target} ${target_pct}% HEAD ${head_pct}%"
|
echo "Results: ${target} ${target_pct}% HEAD ${head_pct}%"
|
||||||
|
|
||||||
delta_pct=$(echo "$head_pct - $target_pct" | bc -l)
|
delta_pct=$(echo "$head_pct - $target_pct" | bc -l)
|
||||||
echo "Delta: ${delta_pct}"
|
echo "Delta: ${delta_pct}"
|
||||||
|
|
||||||
if [[ $delta_pct = \-* ]]; then
|
if [[ $delta_pct = \-* ]]; then
|
||||||
echo "Regression!";
|
echo "Regression!";
|
||||||
return 1
|
|
||||||
|
target_diff="${output_dir}/target.diff.txt"
|
||||||
|
head_diff="${output_dir}/head.diff.txt"
|
||||||
|
cat "${target_out}" | grep -E '^github.com/pelletier/go-toml' | tr -s "\t " | cut -f 2,3 | sort > "${target_diff}"
|
||||||
|
cat "${head_out}" | grep -E '^github.com/pelletier/go-toml' | tr -s "\t " | cut -f 2,3 | sort > "${head_diff}"
|
||||||
|
|
||||||
|
diff --side-by-side --suppress-common-lines "${target_diff}" "${head_diff}"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
|||||||
@@ -130,7 +130,11 @@ func parseDateTime(b []byte) (time.Time, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
seconds := direction * (hours*3600 + minutes*60)
|
seconds := direction * (hours*3600 + minutes*60)
|
||||||
zone = time.FixedZone("", seconds)
|
if seconds == 0 {
|
||||||
|
zone = time.UTC
|
||||||
|
} else {
|
||||||
|
zone = time.FixedZone("", seconds)
|
||||||
|
}
|
||||||
b = b[dateTimeByteLen:]
|
b = b[dateTimeByteLen:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//go:build go1.18
|
||||||
|
// +build go1.18
|
||||||
|
|
||||||
|
package toml_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FuzzUnmarshal(f *testing.F) {
|
||||||
|
file, err := ioutil.ReadFile("benchmark/benchmark.toml")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
f.Add(file)
|
||||||
|
|
||||||
|
f.Fuzz(func(t *testing.T, b []byte) {
|
||||||
|
if strings.Contains(string(b), "nan") {
|
||||||
|
// Current limitation of testify.
|
||||||
|
// https://github.com/stretchr/testify/issues/624
|
||||||
|
t.Skip("can't compare NaNs")
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("INITIAL DOCUMENT ===========================")
|
||||||
|
t.Log(string(b))
|
||||||
|
|
||||||
|
var v interface{}
|
||||||
|
err := toml.Unmarshal(b, &v)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("DECODED VALUE ===========================")
|
||||||
|
t.Logf("%#+v", v)
|
||||||
|
|
||||||
|
encoded, err := toml.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cannot marshal unmarshaled document: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("ENCODED DOCUMENT ===========================")
|
||||||
|
t.Log(string(encoded))
|
||||||
|
|
||||||
|
var v2 interface{}
|
||||||
|
err = toml.Unmarshal(encoded, &v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed round trip: %s", err)
|
||||||
|
}
|
||||||
|
require.Equal(t, v, v2)
|
||||||
|
})
|
||||||
|
}
|
||||||
+65
-47
@@ -208,11 +208,20 @@ func (ctx *encoderCtx) isRoot() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, error) {
|
func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, error) {
|
||||||
if !v.IsZero() {
|
i := v.Interface()
|
||||||
i, ok := v.Interface().(time.Time)
|
|
||||||
if ok {
|
switch x := i.(type) {
|
||||||
return i.AppendFormat(b, time.RFC3339), nil
|
case time.Time:
|
||||||
|
if x.Nanosecond() > 0 {
|
||||||
|
return x.AppendFormat(b, time.RFC3339Nano), nil
|
||||||
}
|
}
|
||||||
|
return x.AppendFormat(b, time.RFC3339), nil
|
||||||
|
case LocalTime:
|
||||||
|
return append(b, x.String()...), nil
|
||||||
|
case LocalDate:
|
||||||
|
return append(b, x.String()...), nil
|
||||||
|
case LocalDateTime:
|
||||||
|
return append(b, x.String()...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
hasTextMarshaler := v.Type().Implements(textMarshalerType)
|
hasTextMarshaler := v.Type().Implements(textMarshalerType)
|
||||||
@@ -260,16 +269,31 @@ func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, e
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
b = enc.encodeString(b, v.String(), ctx.options)
|
b = enc.encodeString(b, v.String(), ctx.options)
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
if math.Trunc(v.Float()) == v.Float() {
|
f := v.Float()
|
||||||
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 32)
|
|
||||||
|
if math.IsNaN(f) {
|
||||||
|
b = append(b, "nan"...)
|
||||||
|
} else if f > math.MaxFloat32 {
|
||||||
|
b = append(b, "inf"...)
|
||||||
|
} else if f < -math.MaxFloat32 {
|
||||||
|
b = append(b, "-inf"...)
|
||||||
|
} else if math.Trunc(f) == f {
|
||||||
|
b = strconv.AppendFloat(b, f, 'f', 1, 32)
|
||||||
} else {
|
} else {
|
||||||
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 32)
|
b = strconv.AppendFloat(b, f, 'f', -1, 32)
|
||||||
}
|
}
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
if math.Trunc(v.Float()) == v.Float() {
|
f := v.Float()
|
||||||
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 64)
|
if math.IsNaN(f) {
|
||||||
|
b = append(b, "nan"...)
|
||||||
|
} else if f > math.MaxFloat64 {
|
||||||
|
b = append(b, "inf"...)
|
||||||
|
} else if f < -math.MaxFloat64 {
|
||||||
|
b = append(b, "-inf"...)
|
||||||
|
} else if math.Trunc(f) == f {
|
||||||
|
b = strconv.AppendFloat(b, f, 'f', 1, 64)
|
||||||
} else {
|
} else {
|
||||||
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 64)
|
b = strconv.AppendFloat(b, f, 'f', -1, 64)
|
||||||
}
|
}
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if v.Bool() {
|
if v.Bool() {
|
||||||
@@ -300,10 +324,6 @@ func isNil(v reflect.Value) bool {
|
|||||||
func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v reflect.Value) ([]byte, error) {
|
func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v reflect.Value) ([]byte, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if !ctx.hasKey {
|
|
||||||
panic("caller of encodeKv should have set the key in the context")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.options.omitempty || options.omitempty) && isEmptyValue(v) {
|
if (ctx.options.omitempty || options.omitempty) && isEmptyValue(v) {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
@@ -313,12 +333,7 @@ func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v r
|
|||||||
}
|
}
|
||||||
|
|
||||||
b = enc.indent(ctx.indent, b)
|
b = enc.indent(ctx.indent, b)
|
||||||
|
b = enc.encodeKey(b, ctx.key)
|
||||||
b, err = enc.encodeKey(b, ctx.key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
b = append(b, " = "...)
|
b = append(b, " = "...)
|
||||||
|
|
||||||
// create a copy of the context because the value of a KV shouldn't
|
// create a copy of the context because the value of a KV shouldn't
|
||||||
@@ -365,7 +380,13 @@ func (enc *Encoder) encodeString(b []byte, v string, options valueOptions) []byt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func needsQuoting(v string) bool {
|
func needsQuoting(v string) bool {
|
||||||
return strings.ContainsAny(v, "'\b\f\n\r\t")
|
// TODO: vectorize
|
||||||
|
for _, b := range []byte(v) {
|
||||||
|
if b == '\'' || b == '\r' || b == '\n' || invalidAscii(b) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// caller should have checked that the string does not contain new lines or ' .
|
// caller should have checked that the string does not contain new lines or ' .
|
||||||
@@ -437,7 +458,7 @@ func (enc *Encoder) encodeQuotedString(multiline bool, b []byte, v string) []byt
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// called should have checked that the string is in A-Z / a-z / 0-9 / - / _ .
|
// caller should have checked that the string is in A-Z / a-z / 0-9 / - / _ .
|
||||||
func (enc *Encoder) encodeUnquotedKey(b []byte, v string) []byte {
|
func (enc *Encoder) encodeUnquotedKey(b []byte, v string) []byte {
|
||||||
return append(b, v...)
|
return append(b, v...)
|
||||||
}
|
}
|
||||||
@@ -453,20 +474,11 @@ func (enc *Encoder) encodeTableHeader(ctx encoderCtx, b []byte) ([]byte, error)
|
|||||||
|
|
||||||
b = append(b, '[')
|
b = append(b, '[')
|
||||||
|
|
||||||
var err error
|
b = enc.encodeKey(b, ctx.parentKey[0])
|
||||||
|
|
||||||
b, err = enc.encodeKey(b, ctx.parentKey[0])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, k := range ctx.parentKey[1:] {
|
for _, k := range ctx.parentKey[1:] {
|
||||||
b = append(b, '.')
|
b = append(b, '.')
|
||||||
|
b = enc.encodeKey(b, k)
|
||||||
b, err = enc.encodeKey(b, k)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b = append(b, "]\n"...)
|
b = append(b, "]\n"...)
|
||||||
@@ -475,19 +487,19 @@ func (enc *Encoder) encodeTableHeader(ctx encoderCtx, b []byte) ([]byte, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//nolint:cyclop
|
//nolint:cyclop
|
||||||
func (enc *Encoder) encodeKey(b []byte, k string) ([]byte, error) {
|
func (enc *Encoder) encodeKey(b []byte, k string) []byte {
|
||||||
needsQuotation := false
|
needsQuotation := false
|
||||||
cannotUseLiteral := false
|
cannotUseLiteral := false
|
||||||
|
|
||||||
|
if len(k) == 0 {
|
||||||
|
return append(b, "''"...)
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range k {
|
for _, c := range k {
|
||||||
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' {
|
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if c == '\n' {
|
|
||||||
return nil, fmt.Errorf("toml: new line characters in keys are not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
if c == literalQuote {
|
if c == literalQuote {
|
||||||
cannotUseLiteral = true
|
cannotUseLiteral = true
|
||||||
}
|
}
|
||||||
@@ -495,13 +507,17 @@ func (enc *Encoder) encodeKey(b []byte, k string) ([]byte, error) {
|
|||||||
needsQuotation = true
|
needsQuotation = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if needsQuotation && needsQuoting(k) {
|
||||||
|
cannotUseLiteral = true
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case cannotUseLiteral:
|
case cannotUseLiteral:
|
||||||
return enc.encodeQuotedString(false, b, k), nil
|
return enc.encodeQuotedString(false, b, k)
|
||||||
case needsQuotation:
|
case needsQuotation:
|
||||||
return enc.encodeLiteralString(b, k), nil
|
return enc.encodeLiteralString(b, k)
|
||||||
default:
|
default:
|
||||||
return enc.encodeUnquotedKey(b, k), nil
|
return enc.encodeUnquotedKey(b, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,7 +616,9 @@ func walkStruct(ctx encoderCtx, t *table, v reflect.Value) {
|
|||||||
|
|
||||||
if k == "" {
|
if k == "" {
|
||||||
if fieldType.Anonymous {
|
if fieldType.Anonymous {
|
||||||
walkStruct(ctx, t, f)
|
if fieldType.Type.Kind() == reflect.Struct {
|
||||||
|
walkStruct(ctx, t, f)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
k = fieldType.Name
|
k = fieldType.Name
|
||||||
@@ -801,6 +819,9 @@ func willConvertToTable(ctx encoderCtx, v reflect.Value) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func willConvertToTableOrArrayTable(ctx encoderCtx, v reflect.Value) bool {
|
func willConvertToTableOrArrayTable(ctx encoderCtx, v reflect.Value) bool {
|
||||||
|
if ctx.insideKv {
|
||||||
|
return false
|
||||||
|
}
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
|
|
||||||
if t.Kind() == reflect.Interface {
|
if t.Kind() == reflect.Interface {
|
||||||
@@ -846,7 +867,6 @@ func (enc *Encoder) encodeSlice(b []byte, ctx encoderCtx, v reflect.Value) ([]by
|
|||||||
func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, error) {
|
func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, error) {
|
||||||
ctx.shiftKey()
|
ctx.shiftKey()
|
||||||
|
|
||||||
var err error
|
|
||||||
scratch := make([]byte, 0, 64)
|
scratch := make([]byte, 0, 64)
|
||||||
scratch = append(scratch, "[["...)
|
scratch = append(scratch, "[["...)
|
||||||
|
|
||||||
@@ -855,10 +875,7 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
|
|||||||
scratch = append(scratch, '.')
|
scratch = append(scratch, '.')
|
||||||
}
|
}
|
||||||
|
|
||||||
scratch, err = enc.encodeKey(scratch, k)
|
scratch = enc.encodeKey(scratch, k)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scratch = append(scratch, "]]\n"...)
|
scratch = append(scratch, "]]\n"...)
|
||||||
@@ -867,6 +884,7 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
|
|||||||
for i := 0; i < v.Len(); i++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
b = append(b, scratch...)
|
b = append(b, scratch...)
|
||||||
|
|
||||||
|
var err error
|
||||||
b, err = enc.encode(b, ctx, v.Index(i))
|
b, err = enc.encode(b, ctx, v.Index(i))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
+81
-6
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -45,7 +46,7 @@ func TestMarshal(t *testing.T) {
|
|||||||
v: map[string]string{
|
v: map[string]string{
|
||||||
"hel\nlo": "world",
|
"hel\nlo": "world",
|
||||||
},
|
},
|
||||||
err: true,
|
expected: `"hel\nlo" = 'world'`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: `map with " in key`,
|
desc: `map with " in key`,
|
||||||
@@ -380,7 +381,8 @@ hello = 'world'`,
|
|||||||
v: map[string][]map[string]string{
|
v: map[string][]map[string]string{
|
||||||
"a\n": {{"hello": "world"}},
|
"a\n": {{"hello": "world"}},
|
||||||
},
|
},
|
||||||
err: true,
|
expected: `[["a\n"]]
|
||||||
|
hello = 'world'`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "newline in map in slice",
|
desc: "newline in map in slice",
|
||||||
@@ -440,7 +442,7 @@ hello = 'world'`,
|
|||||||
v: map[string]interface{}{
|
v: map[string]interface{}{
|
||||||
"hello\nworld": 42,
|
"hello\nworld": 42,
|
||||||
},
|
},
|
||||||
err: true,
|
expected: `"hello\nworld" = 42`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "new line in parent of nested table key",
|
desc: "new line in parent of nested table key",
|
||||||
@@ -449,7 +451,8 @@ hello = 'world'`,
|
|||||||
"inner": 42,
|
"inner": 42,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
err: true,
|
expected: `["hello\nworld"]
|
||||||
|
inner = 42`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "new line in nested table key",
|
desc: "new line in nested table key",
|
||||||
@@ -460,7 +463,9 @@ hello = 'world'`,
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
err: true,
|
expected: `[parent]
|
||||||
|
[parent."in\ner"]
|
||||||
|
foo = 42`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "invalid map key",
|
desc: "invalid map key",
|
||||||
@@ -483,7 +488,16 @@ hello = 'world'`,
|
|||||||
}{
|
}{
|
||||||
T: time.Time{},
|
T: time.Time{},
|
||||||
},
|
},
|
||||||
expected: `T = '0001-01-01T00:00:00Z'`,
|
expected: `T = 0001-01-01T00:00:00Z`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "time nano",
|
||||||
|
v: struct {
|
||||||
|
T time.Time
|
||||||
|
}{
|
||||||
|
T: time.Date(1979, time.May, 27, 0, 32, 0, 999999000, time.UTC),
|
||||||
|
},
|
||||||
|
expected: `T = 1979-05-27T00:32:00.999999Z`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "bool",
|
desc: "bool",
|
||||||
@@ -656,6 +670,33 @@ func equalStringsIgnoreNewlines(t *testing.T, expected string, actual string) {
|
|||||||
assert.Equal(t, strings.Trim(expected, cutset), strings.Trim(actual, cutset))
|
assert.Equal(t, strings.Trim(expected, cutset), strings.Trim(actual, cutset))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarshalFloats(t *testing.T) {
|
||||||
|
v := map[string]float32{
|
||||||
|
"nan": float32(math.NaN()),
|
||||||
|
"+inf": float32(math.Inf(1)),
|
||||||
|
"-inf": float32(math.Inf(-1)),
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `'+inf' = inf
|
||||||
|
-inf = -inf
|
||||||
|
nan = nan
|
||||||
|
`
|
||||||
|
|
||||||
|
actual, err := toml.Marshal(v)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expected, string(actual))
|
||||||
|
|
||||||
|
v64 := map[string]float64{
|
||||||
|
"nan": math.NaN(),
|
||||||
|
"+inf": math.Inf(1),
|
||||||
|
"-inf": math.Inf(-1),
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err = toml.Marshal(v64)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expected, string(actual))
|
||||||
|
}
|
||||||
|
|
||||||
//nolint:funlen
|
//nolint:funlen
|
||||||
func TestMarshalIndentTables(t *testing.T) {
|
func TestMarshalIndentTables(t *testing.T) {
|
||||||
examples := []struct {
|
examples := []struct {
|
||||||
@@ -947,6 +988,22 @@ func TestIssue678(t *testing.T) {
|
|||||||
require.Equal(t, cfg, cfg2)
|
require.Equal(t, cfg, cfg2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssue752(t *testing.T) {
|
||||||
|
type Fooer interface {
|
||||||
|
Foo() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Container struct {
|
||||||
|
Fooer
|
||||||
|
}
|
||||||
|
|
||||||
|
c := Container{}
|
||||||
|
|
||||||
|
out, err := toml.Marshal(c)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "", string(out))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMarshalNestedAnonymousStructs(t *testing.T) {
|
func TestMarshalNestedAnonymousStructs(t *testing.T) {
|
||||||
type Embedded struct {
|
type Embedded struct {
|
||||||
Value string `toml:"value" json:"value"`
|
Value string `toml:"value" json:"value"`
|
||||||
@@ -1011,6 +1068,24 @@ value = ''
|
|||||||
require.Equal(t, expected, string(result))
|
require.Equal(t, expected, string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocalTime(t *testing.T) {
|
||||||
|
v := map[string]toml.LocalTime{
|
||||||
|
"a": toml.LocalTime{
|
||||||
|
Hour: 1,
|
||||||
|
Minute: 2,
|
||||||
|
Second: 3,
|
||||||
|
Nanosecond: 4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `a = 01:02:03.000000004
|
||||||
|
`
|
||||||
|
|
||||||
|
out, err := toml.Marshal(v)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expected, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleMarshal() {
|
func ExampleMarshal() {
|
||||||
type MyConfig struct {
|
type MyConfig struct {
|
||||||
Version int
|
Version int
|
||||||
|
|||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=0000-01-01 00:00:00")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("\"\\n\"=\"\"")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("''=0")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=0000-01-01")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=\"\"\"\\U00000000\"\"\"")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=[[{}]]")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("\"\\b\"=\"\"")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=inf")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=0000-01-01 00:00:00+00:00")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=[{}]")
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("0=nan")
|
||||||
+96
-17
@@ -230,15 +230,6 @@ func (d *decoder) fromParser(root reflect.Value) error {
|
|||||||
return d.p.Error()
|
return d.p.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Rules for the unmarshal code:
|
|
||||||
|
|
||||||
- The stack is used to keep track of which values need to be set where.
|
|
||||||
- handle* functions <=> switch on a given ast.Kind.
|
|
||||||
- unmarshalX* functions need to unmarshal a node of kind X.
|
|
||||||
- An "object" is either a struct or a map.
|
|
||||||
*/
|
|
||||||
|
|
||||||
func (d *decoder) handleRootExpression(expr *ast.Node, v reflect.Value) error {
|
func (d *decoder) handleRootExpression(expr *ast.Node, v reflect.Value) error {
|
||||||
var x reflect.Value
|
var x reflect.Value
|
||||||
var err error
|
var err error
|
||||||
@@ -322,10 +313,12 @@ func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Val
|
|||||||
return v, nil
|
return v, nil
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
elemType := v.Type().Elem()
|
elemType := v.Type().Elem()
|
||||||
|
var elem reflect.Value
|
||||||
if elemType.Kind() == reflect.Interface {
|
if elemType.Kind() == reflect.Interface {
|
||||||
elemType = mapStringInterfaceType
|
elem = makeMapStringInterface()
|
||||||
|
} else {
|
||||||
|
elem = reflect.New(elemType).Elem()
|
||||||
}
|
}
|
||||||
elem := reflect.New(elemType).Elem()
|
|
||||||
elem2, err := d.handleArrayTable(key, elem)
|
elem2, err := d.handleArrayTable(key, elem)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return reflect.Value{}, err
|
return reflect.Value{}, err
|
||||||
@@ -398,6 +391,84 @@ func (d *decoder) handleArrayTableCollection(key ast.Iterator, v reflect.Value)
|
|||||||
return d.handleArrayTable(key, v)
|
return d.handleArrayTable(key, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *decoder) handleKeyValuePartMapStringInterface(key ast.Iterator, value *ast.Node, m map[string]interface{}) (reflect.Value, error) {
|
||||||
|
k := string(key.Node().Data)
|
||||||
|
|
||||||
|
newMap := false
|
||||||
|
if m == nil {
|
||||||
|
newMap = true
|
||||||
|
m = make(map[string]interface{}, 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
set := false
|
||||||
|
v, ok := m[k]
|
||||||
|
if !ok || key.IsLast() {
|
||||||
|
set = true
|
||||||
|
v = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mv := reflect.ValueOf(&v).Elem()
|
||||||
|
|
||||||
|
x, err := d.handleKeyValueInner(key, value, mv)
|
||||||
|
if err != nil {
|
||||||
|
return reflect.Value{}, err
|
||||||
|
}
|
||||||
|
if x.IsValid() {
|
||||||
|
mv = x
|
||||||
|
set = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if set {
|
||||||
|
m[k] = mv.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
if newMap {
|
||||||
|
return reflect.ValueOf(m), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Value{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *decoder) handleKeyPartMapStringInterface(key ast.Iterator, m map[string]interface{}, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
|
||||||
|
newMap := false
|
||||||
|
|
||||||
|
k := string(key.Node().Data)
|
||||||
|
if m == nil {
|
||||||
|
newMap = true
|
||||||
|
m = make(map[string]interface{}, 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := m[k]
|
||||||
|
set := false
|
||||||
|
|
||||||
|
if !ok || v == nil {
|
||||||
|
set = true
|
||||||
|
v = makeFn().Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
mv := reflect.ValueOf(v)
|
||||||
|
|
||||||
|
x, err := nextFn(key, mv)
|
||||||
|
if err != nil {
|
||||||
|
return reflect.Value{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if x.IsValid() {
|
||||||
|
mv = x
|
||||||
|
set = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if set {
|
||||||
|
m[k] = mv.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
if newMap {
|
||||||
|
return reflect.ValueOf(m), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Value{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
|
func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
|
||||||
var rv reflect.Value
|
var rv reflect.Value
|
||||||
|
|
||||||
@@ -414,6 +485,11 @@ func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handle
|
|||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
vt := v.Type()
|
vt := v.Type()
|
||||||
|
|
||||||
|
if vt == mapStringInterfaceType {
|
||||||
|
m := v.Interface().(map[string]interface{})
|
||||||
|
return d.handleKeyPartMapStringInterface(key, m, nextFn, makeFn)
|
||||||
|
}
|
||||||
|
|
||||||
// Create the key for the map element. Convert to key type.
|
// Create the key for the map element. Convert to key type.
|
||||||
mk := reflect.ValueOf(string(key.Node().Data)).Convert(vt.Key())
|
mk := reflect.ValueOf(string(key.Node().Data)).Convert(vt.Key())
|
||||||
|
|
||||||
@@ -997,6 +1073,11 @@ func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflec
|
|||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
vt := v.Type()
|
vt := v.Type()
|
||||||
|
|
||||||
|
if vt == mapStringInterfaceType {
|
||||||
|
m := v.Interface().(map[string]interface{})
|
||||||
|
return d.handleKeyValuePartMapStringInterface(key, value, m)
|
||||||
|
}
|
||||||
|
|
||||||
mk := reflect.ValueOf(string(key.Node().Data))
|
mk := reflect.ValueOf(string(key.Node().Data))
|
||||||
mkt := stringType
|
mkt := stringType
|
||||||
|
|
||||||
@@ -1020,12 +1101,10 @@ func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflec
|
|||||||
if !mv.IsValid() {
|
if !mv.IsValid() {
|
||||||
set = true
|
set = true
|
||||||
mv = reflect.New(v.Type().Elem()).Elem()
|
mv = reflect.New(v.Type().Elem()).Elem()
|
||||||
} else {
|
} else if key.IsLast() {
|
||||||
if key.IsLast() {
|
var x interface{}
|
||||||
var x interface{}
|
mv = reflect.ValueOf(&x).Elem()
|
||||||
mv = reflect.ValueOf(&x).Elem()
|
set = true
|
||||||
set = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nv, err := d.handleKeyValueInner(key, value, mv)
|
nv, err := d.handleKeyValueInner(key, value, mv)
|
||||||
|
|||||||
+2
-2
@@ -971,7 +971,7 @@ B = "data"`,
|
|||||||
"Name": "Hammer",
|
"Name": "Hammer",
|
||||||
"Sku": int64(738594937),
|
"Sku": int64(738594937),
|
||||||
},
|
},
|
||||||
map[string]interface{}(nil),
|
map[string]interface{}{},
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"Name": "Nail",
|
"Name": "Nail",
|
||||||
"Sku": int64(284758393),
|
"Sku": int64(284758393),
|
||||||
@@ -1505,7 +1505,7 @@ B = "data"`,
|
|||||||
target: &map[string]interface{}{},
|
target: &map[string]interface{}{},
|
||||||
expected: &map[string]interface{}{
|
expected: &map[string]interface{}{
|
||||||
"products": []interface{}{
|
"products": []interface{}{
|
||||||
map[string]interface{}(nil),
|
map[string]interface{}{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user