Compare commits

...

9 Commits

Author SHA1 Message Date
Thomas Pelletier 8f6d0d8be7 Specialize map[string]iface when unmarshaling kvs
name                               old time/op    new time/op    delta
UnmarshalDataset/config-8            12.3ms ± 0%    10.9ms ± 0%  -11.36%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            55.2ms ± 0%    55.4ms ± 1%     ~     (p=0.690 n=5+5)
UnmarshalDataset/citm_catalog-8      16.5ms ± 1%    15.7ms ± 0%   -4.43%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           7.01ms ± 1%    6.63ms ± 0%   -5.34%  (p=0.008 n=5+5)
UnmarshalDataset/code-8              52.0ms ± 0%    48.7ms ± 2%   -6.43%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            119µs ± 0%     110µs ± 3%   -6.81%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8     432ns ± 1%     431ns ± 1%     ~     (p=0.222 n=5+5)
Unmarshal/SimpleDocument/map-8        573ns ± 1%     540ns ± 1%   -5.64%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8     33.7µs ± 0%    33.6µs ± 0%     ~     (p=0.310 n=5+5)
Unmarshal/ReferenceFile/map-8        44.2µs ± 1%    41.7µs ± 1%   -5.63%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8          7.28µs ± 1%    6.74µs ± 1%   -7.42%  (p=0.008 n=5+5)
[Geo mean]                            292µs          277µs        -4.91%

name                               old speed      new speed      delta
UnmarshalDataset/config-8          85.2MB/s ± 0%  96.1MB/s ± 0%  +12.82%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8          39.9MB/s ± 0%  39.8MB/s ± 1%     ~     (p=0.690 n=5+5)
UnmarshalDataset/citm_catalog-8    33.9MB/s ± 1%  35.5MB/s ± 0%   +4.64%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8         63.1MB/s ± 1%  66.6MB/s ± 0%   +5.65%  (p=0.008 n=5+5)
UnmarshalDataset/code-8            51.6MB/s ± 0%  55.1MB/s ± 2%   +6.88%  (p=0.008 n=5+5)
UnmarshalDataset/example-8         68.3MB/s ± 0%  73.4MB/s ± 3%   +7.34%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8  25.4MB/s ± 1%  25.5MB/s ± 1%     ~     (p=0.246 n=5+5)
Unmarshal/SimpleDocument/map-8     19.2MB/s ± 1%  20.4MB/s ± 1%   +5.99%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8    156MB/s ± 0%   156MB/s ± 0%     ~     (p=0.310 n=5+5)
Unmarshal/ReferenceFile/map-8       119MB/s ± 1%   126MB/s ± 1%   +5.97%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8        75.0MB/s ± 1%  81.0MB/s ± 1%   +8.01%  (p=0.008 n=5+5)
[Geo mean]                         56.1MB/s       59.0MB/s        +5.17%

name                               old alloc/op   new alloc/op   delta
UnmarshalDataset/config-8            5.26MB ± 0%    4.75MB ± 0%   -9.66%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            83.0MB ± 0%    83.0MB ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8      34.7MB ± 0%    34.3MB ± 0%   -1.13%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           12.7MB ± 0%    12.5MB ± 0%   -1.44%  (p=0.008 n=5+5)
UnmarshalDataset/code-8              15.3MB ± 0%    13.9MB ± 0%   -9.27%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            186kB ± 0%     182kB ± 0%   -2.20%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      805B ± 0%      805B ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8       1.13kB ± 0%    1.12kB ± 0%   -1.41%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8     20.9kB ± 0%    20.9kB ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8        36.4kB ± 0%    35.4kB ± 0%     ~     (p=0.079 n=4+5)
Unmarshal/HugoFrontMatter-8          7.20kB ± 0%    6.98kB ± 0%   -3.11%  (p=0.008 n=5+5)
[Geo mean]                            312kB          303kB        -2.86%

name                               old allocs/op  new allocs/op  delta
UnmarshalDataset/config-8              189k ± 0%      157k ± 0%  -16.80%  (p=0.029 n=4+4)
UnmarshalDataset/canada-8              782k ± 0%      782k ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8        191k ± 0%      167k ± 0%  -12.75%  (p=0.000 n=4+5)
UnmarshalDataset/twitter-8            56.9k ± 0%     45.5k ± 0%  -20.02%  (p=0.016 n=5+4)
UnmarshalDataset/code-8                626k ± 0%      537k ± 0%  -14.22%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            1.36k ± 0%     1.11k ± 0%  -18.53%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      9.00 ± 0%      9.00 ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8         13.0 ± 0%      12.0 ± 0%   -7.69%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8        183 ± 0%       183 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8           526 ± 0%       466 ± 0%  -11.41%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8             126 ± 0%       112 ± 0%  -11.11%  (p=0.008 n=5+5)
[Geo mean]                            3.73k          3.34k       -10.51%
2022-04-15 17:41:18 -04:00
Thomas Pelletier 3c4b709fed Specialize navigating map[string]interface{}
This is a common type. Specializing it to reduce the use of reflection
yields better performance.

Similar to https://github.com/pelletier/go-toml/pull/669, there is a lot
to explore there.

name                               old time/op    new time/op    delta
UnmarshalDataset/config-8            13.3ms ± 0%    12.3ms ± 0%   -7.45%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            55.7ms ± 0%    55.2ms ± 0%   -0.88%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8      16.6ms ± 1%    16.5ms ± 1%   -0.97%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           7.10ms ± 1%    7.01ms ± 1%   -1.28%  (p=0.016 n=5+5)
UnmarshalDataset/code-8              63.8ms ± 0%    52.0ms ± 0%  -18.45%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            121µs ± 0%     119µs ± 0%   -2.13%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8     438ns ± 1%     432ns ± 1%   -1.40%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/map-8        594ns ± 2%     573ns ± 1%   -3.56%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8     34.3µs ± 1%    33.7µs ± 0%   -1.95%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/map-8        48.6µs ± 0%    44.2µs ± 1%   -9.22%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8          7.88µs ± 1%    7.28µs ± 1%   -7.66%  (p=0.008 n=5+5)

name                               old speed      new speed      delta
UnmarshalDataset/config-8          78.9MB/s ± 0%  85.2MB/s ± 0%   +8.05%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8          39.5MB/s ± 0%  39.9MB/s ± 0%   +0.89%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8    33.6MB/s ± 1%  33.9MB/s ± 1%   +0.98%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8         62.3MB/s ± 1%  63.1MB/s ± 1%   +1.30%  (p=0.016 n=5+5)
UnmarshalDataset/code-8            42.1MB/s ± 0%  51.6MB/s ± 0%  +22.62%  (p=0.008 n=5+5)
UnmarshalDataset/example-8         66.9MB/s ± 0%  68.3MB/s ± 0%   +2.18%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8  25.1MB/s ± 1%  25.4MB/s ± 1%   +1.43%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/map-8     18.5MB/s ± 2%  19.2MB/s ± 1%   +3.70%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8    153MB/s ± 1%   156MB/s ± 0%   +1.99%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/map-8       108MB/s ± 0%   119MB/s ± 1%  +10.16%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8        69.3MB/s ± 1%  75.0MB/s ± 1%   +8.30%  (p=0.008 n=5+5)

name                               old alloc/op   new alloc/op   delta
UnmarshalDataset/config-8            5.86MB ± 0%    5.26MB ± 0%  -10.36%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            83.0MB ± 0%    83.0MB ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8      34.7MB ± 0%    34.7MB ± 0%   -0.04%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           12.7MB ± 0%    12.7MB ± 0%     ~     (p=0.548 n=5+5)
UnmarshalDataset/code-8              22.2MB ± 0%    15.3MB ± 0%  -30.76%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            186kB ± 0%     186kB ± 0%   -0.04%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      805B ± 0%      805B ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8       1.13kB ± 0%    1.13kB ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/struct-8     20.9kB ± 0%    20.9kB ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8        38.2kB ± 0%    36.4kB ± 0%   -4.86%  (p=0.029 n=4+4)
Unmarshal/HugoFrontMatter-8          7.44kB ± 0%    7.20kB ± 0%   -3.23%  (p=0.008 n=5+5)

name                               old allocs/op  new allocs/op  delta
UnmarshalDataset/config-8              227k ± 0%      189k ± 0%  -16.74%  (p=0.029 n=4+4)
UnmarshalDataset/canada-8              782k ± 0%      782k ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8        192k ± 0%      191k ± 0%   -0.49%  (p=0.000 n=5+4)
UnmarshalDataset/twitter-8            56.9k ± 0%     56.9k ± 0%   -0.00%  (p=0.032 n=5+5)
UnmarshalDataset/code-8               1.05M ± 0%     0.63M ± 0%  -40.52%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            1.36k ± 0%     1.36k ± 0%   -0.15%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      9.00 ± 0%      9.00 ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8         13.0 ± 0%      13.0 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/struct-8        183 ± 0%       183 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8           642 ± 0%       526 ± 0%  -18.07%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8             141 ± 0%       126 ± 0%  -10.64%  (p=0.008 n=5+5)
2022-04-15 17:20:24 -04:00
Thomas Pelletier adacebd8c7 Update benchmarks in README (#756) 2022-04-10 22:24:19 -04:00
Thomas Pelletier 8bbb673431 Fuzzing setup and fixes (#755)
* encode: fix localdate formatting
* encode: fix empty key marshaling
* encode: fix invalid quotation of time.Time
* encode: ensure control chars are escaped
* decode: always use UTC for zero tz
* encode: check for invalid characters in keys
* encode: always construct map for empty array tables
* fuzz: add go 1.18 fuzz test
* encode: handle NaNs
* encode: allow new lines in quoted keys
* encode: never emit table inside array
* encode: don't capitalize inf
2022-04-10 21:37:12 -04:00
Thomas Pelletier 2377ac4bc0 encode: fix embedded interfaces (#753)
Resolve marshaling regression when handling an embedded interface in a
struct.

Fixes #752
2022-04-08 09:25:54 -04:00
Thomas Pelletier f5cc8c49eb decoder: remove mention of UnmarshalText in errors (#751)
Fixes #737
2022-04-07 21:58:19 -04:00
Thomas Pelletier 89d7b412d8 decode: allow subtables to be defined later (#750)
Fixes #739
2022-04-07 21:49:16 -04:00
Thomas Pelletier 88a8aecdd4 tools: display error context when it exists (#749)
For example when failing to decode toml, display the context around the
error and the location of the problem.
2022-04-07 20:33:09 -04:00
Thomas Pelletier 9804fc57e0 decoder: support \e escape sequence (#748) 2022-04-07 20:18:30 -04:00
26 changed files with 537 additions and 111 deletions
+1
View File
@@ -1,3 +1,4 @@
* text=auto
benchmark/benchmark.toml text eol=lf
testdata/** text eol=lf
+16 -16
View File
@@ -161,11 +161,11 @@ Execution time speedup compared to other Go TOML libraries:
</thead>
<tbody>
<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/struct-2</td><td>2.4x</td><td>2.6x</td></tr>
<tr><td>Unmarshal/HugoFrontMatter-2</td><td>2.9x</td><td>2.5x</td></tr>
<tr><td>Unmarshal/ReferenceFile/map-2</td><td>2.7x</td><td>2.6x</td></tr>
<tr><td>Unmarshal/ReferenceFile/struct-2</td><td>4.8x</td><td>5.1x</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.2x</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.6x</td><td>2.9x</td></tr>
<tr><td>Unmarshal/ReferenceFile/struct-2</td><td>4.4x</td><td>5.3x</td></tr>
</tbody>
</table>
<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>
</thead>
<tbody>
<tr><td>Marshal/SimpleDocument/map-2</td><td>1.7x</td><td>2.1x</td></tr>
<tr><td>Marshal/SimpleDocument/struct-2</td><td>2.5x</td><td>2.8x</td></tr>
<tr><td>Unmarshal/SimpleDocument/map-2</td><td>4.1x</td><td>3.1x</td></tr>
<tr><td>Unmarshal/SimpleDocument/struct-2</td><td>6.4x</td><td>4.3x</td></tr>
<tr><td>UnmarshalDataset/example-2</td><td>3.4x</td><td>3.2x</td></tr>
<tr><td>UnmarshalDataset/code-2</td><td>2.2x</td><td>2.5x</td></tr>
<tr><td>UnmarshalDataset/twitter-2</td><td>2.8x</td><td>2.7x</td></tr>
<tr><td>UnmarshalDataset/citm_catalog-2</td><td>2.2x</td><td>2.0x</td></tr>
<tr><td>UnmarshalDataset/canada-2</td><td>1.8x</td><td>1.4x</td></tr>
<tr><td>UnmarshalDataset/config-2</td><td>4.4x</td><td>2.9x</td></tr>
<tr><td>[Geo mean]</td><td>2.8x</td><td>2.6x</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.7x</td><td>4.2x</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.2x</td><td>3.9x</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.3x</td><td>3.1x</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.1x</td><td>2.2x</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.3x</td><td>3.2x</td></tr>
<tr><td>[Geo mean]</td><td>2.7x</td><td>2.8x</td></tr>
</tbody>
</table>
<p>This table can be generated with <code>./ci.sh benchmark -a -html</code>.</p>
+11 -3
View File
@@ -76,7 +76,8 @@ cover() {
fi
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
popd
@@ -103,8 +104,8 @@ coverage() {
echo ""
target_pct="$(cat ${target_out} |sed -E 's/.*total.*\t([0-9.]+)%/\1/;t;d')"
head_pct="$(cat ${head_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="$(tail -n2 ${head_out} | head -n1 | sed -E 's/.*total.*\t([0-9.]+)%/\1/')"
echo "Results: ${target} ${target_pct}% HEAD ${head_pct}%"
delta_pct=$(echo "$head_pct - $target_pct" | bc -l)
@@ -112,6 +113,13 @@ coverage() {
if [[ $delta_pct = \-* ]]; then
echo "Regression!";
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
return 0
+4
View File
@@ -130,7 +130,11 @@ func parseDateTime(b []byte) (time.Time, error) {
}
seconds := direction * (hours*3600 + minutes*60)
if seconds == 0 {
zone = time.UTC
} else {
zone = time.FixedZone("", seconds)
}
b = b[dateTimeByteLen:]
}
+56
View File
@@ -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)
})
}
+12
View File
@@ -2,11 +2,14 @@ package cli
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/pelletier/go-toml/v2"
)
type ConvertFn func(r io.Reader, w io.Writer) error
@@ -28,7 +31,16 @@ func (p *Program) Execute() {
func (p *Program) main(files []string, input io.Reader, output, error io.Writer) int {
err := p.run(files, input, output)
if err != nil {
var derr *toml.DecodeError
if errors.As(err, &derr) {
fmt.Fprintln(error, derr.String())
row, col := derr.Position()
fmt.Fprintln(error, "error occurred at row", row, "column", col)
} else {
fmt.Fprintln(error, err.Error())
}
return -1
}
return 0
+16
View File
@@ -10,6 +10,7 @@ import (
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -47,6 +48,21 @@ func TestProcessMainStdinErr(t *testing.T) {
assert.NotEmpty(t, stderr.String())
}
func TestProcessMainStdinDecodeErr(t *testing.T) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
input := strings.NewReader("this is the input")
exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error {
var v interface{}
return toml.Unmarshal([]byte(`qwe = 001`), &v)
})
assert.Equal(t, -1, exit)
assert.Empty(t, stdout.String())
assert.Contains(t, stderr.String(), "error occurred at")
}
func TestProcessMainFileExists(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "example")
require.NoError(t, err)
+11 -6
View File
@@ -79,6 +79,7 @@ type entry struct {
name []byte
kind keyKind
explicit bool
kv bool
}
// Find the index of the child of parentIdx with key k. Returns -1 if
@@ -111,7 +112,7 @@ func (s *SeenTracker) clear(idx int) {
s.entries[idx].child = -1
}
func (s *SeenTracker) create(parentIdx int, name []byte, kind keyKind, explicit bool) int {
func (s *SeenTracker) create(parentIdx int, name []byte, kind keyKind, explicit bool, kv bool) int {
e := entry{
child: -1,
next: s.entries[parentIdx].child,
@@ -119,6 +120,7 @@ func (s *SeenTracker) create(parentIdx int, name []byte, kind keyKind, explicit
name: name,
kind: kind,
explicit: explicit,
kv: kv,
}
var idx int
if s.entries[0].next >= 0 {
@@ -137,7 +139,10 @@ func (s *SeenTracker) create(parentIdx int, name []byte, kind keyKind, explicit
func (s *SeenTracker) setExplicitFlag(parentIdx int) {
for i := s.entries[parentIdx].child; i >= 0; i = s.entries[i].next {
if s.entries[i].kv {
s.entries[i].explicit = true
s.entries[i].kv = false
}
s.setExplicitFlag(i)
}
}
@@ -183,7 +188,7 @@ func (s *SeenTracker) checkTable(node *ast.Node) error {
idx := s.find(parentIdx, k)
if idx < 0 {
idx = s.create(parentIdx, k, tableKind, false)
idx = s.create(parentIdx, k, tableKind, false, false)
} else {
entry := s.entries[idx]
if entry.kind == valueKind {
@@ -206,7 +211,7 @@ func (s *SeenTracker) checkTable(node *ast.Node) error {
}
s.entries[idx].explicit = true
} else {
idx = s.create(parentIdx, k, tableKind, true)
idx = s.create(parentIdx, k, tableKind, true, false)
}
s.currentIdx = idx
@@ -233,7 +238,7 @@ func (s *SeenTracker) checkArrayTable(node *ast.Node) error {
idx := s.find(parentIdx, k)
if idx < 0 {
idx = s.create(parentIdx, k, tableKind, false)
idx = s.create(parentIdx, k, tableKind, false, false)
} else {
entry := s.entries[idx]
if entry.kind == valueKind {
@@ -254,7 +259,7 @@ func (s *SeenTracker) checkArrayTable(node *ast.Node) error {
}
s.clear(idx)
} else {
idx = s.create(parentIdx, k, arrayTableKind, true)
idx = s.create(parentIdx, k, arrayTableKind, true, false)
}
s.currentIdx = idx
@@ -272,7 +277,7 @@ func (s *SeenTracker) checkKeyValue(node *ast.Node) error {
idx := s.find(parentIdx, k)
if idx < 0 {
idx = s.create(parentIdx, k, tableKind, false)
idx = s.create(parentIdx, k, tableKind, false, true)
} else {
entry := s.entries[idx]
if it.IsLast() {
+15
View File
@@ -0,0 +1,15 @@
package tracker
import (
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
func TestEntrySize(t *testing.T) {
// Validate no regression on the size of entry{}. This is a critical bit for
// performance of unmarshaling documents. Should only be increased with care
// and a very good reason.
require.LessOrEqual(t, 48, int(unsafe.Sizeof(entry{})))
}
+64 -46
View File
@@ -208,11 +208,20 @@ func (ctx *encoderCtx) isRoot() bool {
}
func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, error) {
if !v.IsZero() {
i, ok := v.Interface().(time.Time)
if ok {
return i.AppendFormat(b, time.RFC3339), nil
i := v.Interface()
switch x := i.(type) {
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)
@@ -260,16 +269,31 @@ func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, e
case reflect.String:
b = enc.encodeString(b, v.String(), ctx.options)
case reflect.Float32:
if math.Trunc(v.Float()) == v.Float() {
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 32)
f := v.Float()
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 {
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 32)
b = strconv.AppendFloat(b, f, 'f', -1, 32)
}
case reflect.Float64:
if math.Trunc(v.Float()) == v.Float() {
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 64)
f := v.Float()
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 {
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 64)
b = strconv.AppendFloat(b, f, 'f', -1, 64)
}
case reflect.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) {
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) {
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, err = enc.encodeKey(b, ctx.key)
if err != nil {
return nil, err
}
b = enc.encodeKey(b, ctx.key)
b = append(b, " = "...)
// 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 {
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 ' .
@@ -437,7 +458,7 @@ func (enc *Encoder) encodeQuotedString(multiline bool, b []byte, v string) []byt
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 {
return append(b, v...)
}
@@ -453,20 +474,11 @@ func (enc *Encoder) encodeTableHeader(ctx encoderCtx, b []byte) ([]byte, error)
b = append(b, '[')
var err error
b, err = enc.encodeKey(b, ctx.parentKey[0])
if err != nil {
return nil, err
}
b = enc.encodeKey(b, ctx.parentKey[0])
for _, k := range ctx.parentKey[1:] {
b = append(b, '.')
b, err = enc.encodeKey(b, k)
if err != nil {
return nil, err
}
b = enc.encodeKey(b, k)
}
b = append(b, "]\n"...)
@@ -475,19 +487,19 @@ func (enc *Encoder) encodeTableHeader(ctx encoderCtx, b []byte) ([]byte, error)
}
//nolint:cyclop
func (enc *Encoder) encodeKey(b []byte, k string) ([]byte, error) {
func (enc *Encoder) encodeKey(b []byte, k string) []byte {
needsQuotation := false
cannotUseLiteral := false
if len(k) == 0 {
return append(b, "''"...)
}
for _, c := range k {
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' {
continue
}
if c == '\n' {
return nil, fmt.Errorf("toml: new line characters in keys are not supported")
}
if c == literalQuote {
cannotUseLiteral = true
}
@@ -495,13 +507,17 @@ func (enc *Encoder) encodeKey(b []byte, k string) ([]byte, error) {
needsQuotation = true
}
if needsQuotation && needsQuoting(k) {
cannotUseLiteral = true
}
switch {
case cannotUseLiteral:
return enc.encodeQuotedString(false, b, k), nil
return enc.encodeQuotedString(false, b, k)
case needsQuotation:
return enc.encodeLiteralString(b, k), nil
return enc.encodeLiteralString(b, k)
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 fieldType.Anonymous {
if fieldType.Type.Kind() == reflect.Struct {
walkStruct(ctx, t, f)
}
continue
} else {
k = fieldType.Name
@@ -801,6 +819,9 @@ func willConvertToTable(ctx encoderCtx, v reflect.Value) bool {
}
func willConvertToTableOrArrayTable(ctx encoderCtx, v reflect.Value) bool {
if ctx.insideKv {
return false
}
t := v.Type()
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) {
ctx.shiftKey()
var err error
scratch := make([]byte, 0, 64)
scratch = append(scratch, "[["...)
@@ -855,10 +875,7 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
scratch = append(scratch, '.')
}
scratch, err = enc.encodeKey(scratch, k)
if err != nil {
return nil, err
}
scratch = enc.encodeKey(scratch, k)
}
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++ {
b = append(b, scratch...)
var err error
b, err = enc.encode(b, ctx, v.Index(i))
if err != nil {
return nil, err
+81 -6
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"math"
"math/big"
"strings"
"testing"
@@ -45,7 +46,7 @@ func TestMarshal(t *testing.T) {
v: map[string]string{
"hel\nlo": "world",
},
err: true,
expected: `"hel\nlo" = 'world'`,
},
{
desc: `map with " in key`,
@@ -380,7 +381,8 @@ hello = 'world'`,
v: map[string][]map[string]string{
"a\n": {{"hello": "world"}},
},
err: true,
expected: `[["a\n"]]
hello = 'world'`,
},
{
desc: "newline in map in slice",
@@ -440,7 +442,7 @@ hello = 'world'`,
v: map[string]interface{}{
"hello\nworld": 42,
},
err: true,
expected: `"hello\nworld" = 42`,
},
{
desc: "new line in parent of nested table key",
@@ -449,7 +451,8 @@ hello = 'world'`,
"inner": 42,
},
},
err: true,
expected: `["hello\nworld"]
inner = 42`,
},
{
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",
@@ -483,7 +488,16 @@ hello = 'world'`,
}{
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",
@@ -656,6 +670,33 @@ func equalStringsIgnoreNewlines(t *testing.T, expected string, actual string) {
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
func TestMarshalIndentTables(t *testing.T) {
examples := []struct {
@@ -947,6 +988,22 @@ func TestIssue678(t *testing.T) {
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) {
type Embedded struct {
Value string `toml:"value" json:"value"`
@@ -1011,6 +1068,24 @@ value = ''
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() {
type MyConfig struct {
Version int
+4
View File
@@ -617,6 +617,8 @@ func (p *parser) parseMultilineBasicString(b []byte) ([]byte, []byte, []byte, er
builder.WriteByte('\r')
case 't':
builder.WriteByte('\t')
case 'e':
builder.WriteByte(0x1B)
case 'u':
x, err := hexToRune(atmost(token[i+1:], 4), 4)
if err != nil {
@@ -774,6 +776,8 @@ func (p *parser) parseBasicString(b []byte) ([]byte, []byte, []byte, error) {
builder.WriteByte('\r')
case 't':
builder.WriteByte('\t')
case 'e':
builder.WriteByte(0x1B)
case 'u':
x, err := hexToRune(token[i+1:len(token)-1], 4)
if err != nil {
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=0000-01-01 00:00:00")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\"\\n\"=\"\"")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("''=0")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=0000-01-01")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=\"\"\"\\U00000000\"\"\"")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=[[{}]]")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\"\\b\"=\"\"")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=inf")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=0000-01-01 00:00:00+00:00")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=[{}]")
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0=nan")
+90 -8
View File
@@ -1,4 +1,4 @@
// Generated by tomltestgen for toml-test ref master on 2021-12-31T17:10:15-05:00
// Generated by tomltestgen for toml-test ref master on 2022-04-07T20:09:42-04:00
package toml_test
import (
@@ -55,11 +55,51 @@ func TestTOMLTest_Invalid_Array_TextInArray(t *testing.T) {
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_AlmostFalseWithExtra(t *testing.T) {
input := "a = falsify\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_AlmostFalse(t *testing.T) {
input := "a = fals\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_AlmostTrueWithExtra(t *testing.T) {
input := "a = truthy\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_AlmostTrue(t *testing.T) {
input := "a = tru\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_JustF(t *testing.T) {
input := "a = f\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_JustT(t *testing.T) {
input := "a = t\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_MixedCase(t *testing.T) {
input := "valid = False\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_StartingSameFalse(t *testing.T) {
input := "a = falsey\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_StartingSameTrue(t *testing.T) {
input := "a = truer\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Bool_WrongCaseFalse(t *testing.T) {
input := "b = FALSE\n"
testgenInvalid(t, input)
@@ -70,11 +110,26 @@ func TestTOMLTest_Invalid_Bool_WrongCaseTrue(t *testing.T) {
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Control_BareCr(t *testing.T) {
input := "# The following line contains a single carriage return control character\r\n\r"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Control_BareFormfeed(t *testing.T) {
input := "bare-formfeed = \f\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Control_BareNull(t *testing.T) {
input := "bare-null = \"some value\" \x00\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Control_BareVerticalTab(t *testing.T) {
input := "bare-vertical-tab = \v\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Control_CommentCr(t *testing.T) {
input := "comment-cr = \"Carriage return in comment\" # \ra=1\n"
testgenInvalid(t, input)
@@ -445,6 +500,11 @@ func TestTOMLTest_Invalid_Float_UsBeforePoint(t *testing.T) {
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_InlineTable_Add(t *testing.T) {
input := "a={}\n# Inline tables are immutable and can't be extended\n[a.b]\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_InlineTable_DoubleComma(t *testing.T) {
input := "t = {x=3,,y=4}\n"
testgenInvalid(t, input)
@@ -525,6 +585,21 @@ func TestTOMLTest_Invalid_Integer_DoubleUs(t *testing.T) {
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Integer_IncompleteBin(t *testing.T) {
input := "incomplete-bin = 0b\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Integer_IncompleteHex(t *testing.T) {
input := "incomplete-hex = 0x\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Integer_IncompleteOct(t *testing.T) {
input := "incomplete-oct = 0o\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_Integer_InvalidBin(t *testing.T) {
input := "invalid-bin = 0b0012\n"
testgenInvalid(t, input)
@@ -910,11 +985,6 @@ func TestTOMLTest_Invalid_String_MultilineQuotes1(t *testing.T) {
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_String_MultilineQuotes2(t *testing.T) {
input := "a = \"\"\"6 quotes: \"\"\"\"\"\"\n"
testgenInvalid(t, input)
}
func TestTOMLTest_Invalid_String_NoClose(t *testing.T) {
input := "no-ending-quote = \"One time, at band camp\n"
testgenInvalid(t, input)
@@ -1471,6 +1541,12 @@ func TestTOMLTest_Valid_String_Empty(t *testing.T) {
testgenValid(t, input, jsonRef)
}
func TestTOMLTest_Valid_String_EscapeEsc(t *testing.T) {
input := "esc = \"\\e There is no escape! \\e\"\n"
jsonRef := "{\n \"esc\": {\n \"type\": \"string\",\n \"value\": \"\\u001b There is no escape! \\u001b\"\n }\n}\n"
testgenValid(t, input, jsonRef)
}
func TestTOMLTest_Valid_String_EscapeTricky(t *testing.T) {
input := "end_esc = \"String does not end here\\\" but ends here\\\\\"\nlit_end_esc = 'String ends here\\'\n\nmultiline_unicode = \"\"\"\n\\u00a0\"\"\"\n\nmultiline_not_unicode = \"\"\"\n\\\\u0041\"\"\"\n\nmultiline_end_esc = \"\"\"When will it end? \\\"\"\"...\"\"\\\" should be here\\\"\"\"\"\n\nlit_multiline_not_unicode = '''\n\\u007f'''\n\nlit_multiline_end = '''There is no escape\\'''\n"
jsonRef := "{\n \"end_esc\": {\n \"type\": \"string\",\n \"value\": \"String does not end here\\\" but ends here\\\\\"\n },\n \"lit_end_esc\": {\n \"type\": \"string\",\n \"value\": \"String ends here\\\\\"\n },\n \"lit_multiline_end\": {\n \"type\": \"string\",\n \"value\": \"There is no escape\\\\\"\n },\n \"lit_multiline_not_unicode\": {\n \"type\": \"string\",\n \"value\": \"\\\\u007f\"\n },\n \"multiline_end_esc\": {\n \"type\": \"string\",\n \"value\": \"When will it end? \\\"\\\"\\\"...\\\"\\\"\\\" should be here\\\"\"\n },\n \"multiline_not_unicode\": {\n \"type\": \"string\",\n \"value\": \"\\\\u0041\"\n },\n \"multiline_unicode\": {\n \"type\": \"string\",\n \"value\": \"\u00a0\"\n }\n}\n"
@@ -1489,9 +1565,15 @@ func TestTOMLTest_Valid_String_Escapes(t *testing.T) {
testgenValid(t, input, jsonRef)
}
func TestTOMLTest_Valid_String_MultilineEscapedCrlf(t *testing.T) {
input := "# The following line should be an unescaped backslash followed by a Windows\r\n# newline sequence (\"\\r\\n\")\r\n0=\"\"\"\\\r\n\"\"\"\r\n"
jsonRef := "{\n \"0\": {\n \"type\": \"string\",\n \"value\": \"\"\n }\n}\n"
testgenValid(t, input, jsonRef)
}
func TestTOMLTest_Valid_String_MultilineQuotes(t *testing.T) {
input := "# Make sure that quotes inside multiline strings are allowed, including right\n# after the opening '''/\"\"\" and before the closing '''/\"\"\"\n\nlit_one = ''''one quote''''\nlit_two = '''''two quotes'''''\nlit_one_space = ''' 'one quote' '''\nlit_two_space = ''' ''two quotes'' '''\n\none = \"\"\"\"one quote\"\"\"\"\ntwo = \"\"\"\"\"two quotes\"\"\"\"\"\none_space = \"\"\" \"one quote\" \"\"\"\ntwo_space = \"\"\" \"\"two quotes\"\" \"\"\"\n\nmismatch1 = \"\"\"aaa'''bbb\"\"\"\nmismatch2 = '''aaa\"\"\"bbb'''\n"
jsonRef := "{\n \"lit_one\": {\n \"type\": \"string\",\n \"value\": \"'one quote'\"\n },\n \"lit_one_space\": {\n \"type\": \"string\",\n \"value\": \" 'one quote' \"\n },\n \"lit_two\": {\n \"type\": \"string\",\n \"value\": \"''two quotes''\"\n },\n \"lit_two_space\": {\n \"type\": \"string\",\n \"value\": \" ''two quotes'' \"\n },\n \"mismatch1\": {\n \"type\": \"string\",\n \"value\": \"aaa'''bbb\"\n },\n \"mismatch2\": {\n \"type\": \"string\",\n \"value\": \"aaa\\\"\\\"\\\"bbb\"\n },\n \"one\": {\n \"type\": \"string\",\n \"value\": \"\\\"one quote\\\"\"\n },\n \"one_space\": {\n \"type\": \"string\",\n \"value\": \" \\\"one quote\\\" \"\n },\n \"two\": {\n \"type\": \"string\",\n \"value\": \"\\\"\\\"two quotes\\\"\\\"\"\n },\n \"two_space\": {\n \"type\": \"string\",\n \"value\": \" \\\"\\\"two quotes\\\"\\\" \"\n }\n}\n"
input := "# Make sure that quotes inside multiline strings are allowed, including right\n# after the opening '''/\"\"\" and before the closing '''/\"\"\"\n\nlit_one = ''''one quote''''\nlit_two = '''''two quotes'''''\nlit_one_space = ''' 'one quote' '''\nlit_two_space = ''' ''two quotes'' '''\n\none = \"\"\"\"one quote\"\"\"\"\ntwo = \"\"\"\"\"two quotes\"\"\"\"\"\none_space = \"\"\" \"one quote\" \"\"\"\ntwo_space = \"\"\" \"\"two quotes\"\" \"\"\"\n\nmismatch1 = \"\"\"aaa'''bbb\"\"\"\nmismatch2 = '''aaa\"\"\"bbb'''\n\n# Three opening \"\"\", then one escaped \", then two \"\" (allowed), and then three\n# closing \"\"\"\nescaped = \"\"\"lol\\\"\"\"\"\"\"\n"
jsonRef := "{\n \"escaped\": {\n \"type\": \"string\",\n \"value\": \"lol\\\"\\\"\\\"\"\n },\n \"lit_one\": {\n \"type\": \"string\",\n \"value\": \"'one quote'\"\n },\n \"lit_one_space\": {\n \"type\": \"string\",\n \"value\": \" 'one quote' \"\n },\n \"lit_two\": {\n \"type\": \"string\",\n \"value\": \"''two quotes''\"\n },\n \"lit_two_space\": {\n \"type\": \"string\",\n \"value\": \" ''two quotes'' \"\n },\n \"mismatch1\": {\n \"type\": \"string\",\n \"value\": \"aaa'''bbb\"\n },\n \"mismatch2\": {\n \"type\": \"string\",\n \"value\": \"aaa\\\"\\\"\\\"bbb\"\n },\n \"one\": {\n \"type\": \"string\",\n \"value\": \"\\\"one quote\\\"\"\n },\n \"one_space\": {\n \"type\": \"string\",\n \"value\": \" \\\"one quote\\\" \"\n },\n \"two\": {\n \"type\": \"string\",\n \"value\": \"\\\"\\\"two quotes\\\"\\\"\"\n },\n \"two_space\": {\n \"type\": \"string\",\n \"value\": \" \\\"\\\"two quotes\\\"\\\" \"\n }\n}\n"
testgenValid(t, input, jsonRef)
}
+94 -15
View File
@@ -230,15 +230,6 @@ func (d *decoder) fromParser(root reflect.Value) 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 {
var x reflect.Value
var err error
@@ -322,10 +313,12 @@ func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Val
return v, nil
case reflect.Slice:
elemType := v.Type().Elem()
var elem reflect.Value
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)
if err != nil {
return reflect.Value{}, err
@@ -398,6 +391,84 @@ func (d *decoder) handleArrayTableCollection(key ast.Iterator, v reflect.Value)
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) {
var rv reflect.Value
@@ -414,6 +485,11 @@ func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handle
case reflect.Map:
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.
mk := reflect.ValueOf(string(key.Node().Data)).Convert(vt.Key())
@@ -611,7 +687,7 @@ func (d *decoder) tryTextUnmarshaler(node *ast.Node, v reflect.Value) (bool, err
if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
err := v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
if err != nil {
return false, newDecodeError(d.p.Raw(node.Raw), "error calling UnmarshalText: %w", err)
return false, newDecodeError(d.p.Raw(node.Raw), "%w", err)
}
return true, nil
@@ -997,6 +1073,11 @@ func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflec
case reflect.Map:
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))
mkt := stringType
@@ -1020,13 +1101,11 @@ func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflec
if !mv.IsValid() {
set = true
mv = reflect.New(v.Type().Elem()).Elem()
} else {
if key.IsLast() {
} else if key.IsLast() {
var x interface{}
mv = reflect.ValueOf(&x).Elem()
set = true
}
}
nv, err := d.handleKeyValueInner(key, value, mv)
if err != nil {
+31 -2
View File
@@ -545,6 +545,35 @@ func TestUnmarshal(t *testing.T) {
}
},
},
{
desc: "issue 739 - table redefinition",
input: `
[foo.bar.baz]
wibble = 'wobble'
[foo]
[foo.bar]
huey = 'dewey'
`,
gen: func() test {
m := map[string]interface{}{}
return test{
target: &m,
expected: &map[string]interface{}{
`foo`: map[string]interface{}{
"bar": map[string]interface{}{
"huey": "dewey",
"baz": map[string]interface{}{
"wibble": "wobble",
},
},
},
},
}
},
},
{
desc: "multiline basic string",
input: `A = """\
@@ -942,7 +971,7 @@ B = "data"`,
"Name": "Hammer",
"Sku": int64(738594937),
},
map[string]interface{}(nil),
map[string]interface{}{},
map[string]interface{}{
"Name": "Nail",
"Sku": int64(284758393),
@@ -1476,7 +1505,7 @@ B = "data"`,
target: &map[string]interface{}{},
expected: &map[string]interface{}{
"products": []interface{}{
map[string]interface{}(nil),
map[string]interface{}{},
},
},
}