Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: BurntSushi/toml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.3.0
Choose a base ref
...
head repository: BurntSushi/toml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.3.1
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 3, 2023

  1. Remove stray comment

    arp242 committed Jun 3, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    arp242 Martin Tournoij
    Copy the full SHA
    c859a22 View commit details

Commits on Jun 5, 2023

  1. Correctly set the Meta.Keys() order for inline tables

    The table name would be listed *after* the key name; e.g. ./cmd/tomlv
    would print:
    
    	    toml-schema.version                              Integer
    	toml-schema                                          Hash
    
    For the document:
    
    	toml-schema = {version = 1}
    
    With this it prints the expected:
    
    	toml-schema                                          Hash
    	    toml-schema.version                              Integer
    arp242 committed Jun 5, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    arp242 Martin Tournoij
    Copy the full SHA
    60801d0 View commit details

Commits on Jun 6, 2023

  1. Fix encode benchmark

    arp242 committed Jun 6, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    arp242 Martin Tournoij
    Copy the full SHA
    743df59 View commit details
  2. Check BURNTSUSHI_TOML_110 in the parser

    Checking it on import means applications won't be to set it with SetEnv
    in the application itself.
    
    Fixes #394
    arp242 committed Jun 6, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    arp242 Martin Tournoij
    Copy the full SHA
    96fcef2 View commit details
Showing with 51 additions and 24 deletions.
  1. +4 −1 bench_test.go
  2. +1 −1 decode.go
  3. +32 −2 decode_test.go
  4. +3 −5 encode.go
  5. +11 −15 parse.go
5 changes: 4 additions & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
@@ -66,7 +66,10 @@ func BenchmarkEncode(b *testing.B) {
}

// "next" version of TOML.
if path == "valid/string/escape-esc.toml" {
switch path {
case "valid/string/escape-esc.toml", "valid/datetime/no-seconds.toml",
"valid/string/hex-escape.toml", "valid/inline-table/newline.toml",
"valid/key/unicode.toml":
return nil
}

2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
@@ -248,7 +248,7 @@ func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
case reflect.Bool:
return md.unifyBool(data, rv)
case reflect.Interface:
if rv.NumMethod() > 0 { // Only support empty interfaces are supported.
if rv.NumMethod() > 0 { /// Only empty interfaces are supported.
return md.e("unsupported type %s", rv.Type())
}
return md.unifyAnything(data, rv)
34 changes: 32 additions & 2 deletions decode_test.go
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ import (
)

func WithTomlNext(f func()) {
tomlNext = true
defer func() { tomlNext = false }()
os.Setenv("BURNTSUSHI_TOML_110", "")
defer func() { os.Unsetenv("BURNTSUSHI_TOML_110") }()
f()
}

@@ -1171,6 +1171,36 @@ func TestDecodeDoubleTags(t *testing.T) {
}
}

func TestMetaKeys(t *testing.T) {
tests := []struct {
in string
want []Key
}{
{"", []Key{}},
{"b=1\na=1", []Key{Key{"b"}, Key{"a"}}},
{"a.b=1\na.a=1", []Key{Key{"a", "b"}, Key{"a", "a"}}}, // TODO: should include "a"
{"[tbl]\na=1", []Key{Key{"tbl"}, Key{"tbl", "a"}}},
{"[tbl]\na.a=1", []Key{Key{"tbl"}, Key{"tbl", "a", "a"}}}, // TODO: should include "a.a"
{"tbl={a=1}", []Key{Key{"tbl"}, Key{"tbl", "a"}}},
{"tbl={a={b=1}}", []Key{Key{"tbl"}, Key{"tbl", "a"}, Key{"tbl", "a", "b"}}},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
var x interface{}
meta, err := Decode(tt.in, &x)
if err != nil {
t.Fatal(err)
}

have := meta.Keys()
if !reflect.DeepEqual(tt.want, have) {
t.Errorf("\nhave: %s\nwant: %s\n", have, tt.want)
}
})
}
}

// errorContains checks if the error message in have contains the text in
// want.
//
8 changes: 3 additions & 5 deletions encode.go
Original file line number Diff line number Diff line change
@@ -136,10 +136,8 @@ func NewEncoder(w io.Writer) *Encoder {
// document.
func (enc *Encoder) Encode(v interface{}) error {
rv := eindirect(reflect.ValueOf(v))

// XXX

if err := enc.safeEncode(Key([]string{}), rv); err != nil {
err := enc.safeEncode(Key([]string{}), rv)
if err != nil {
return err
}
return enc.w.Flush()
@@ -505,7 +503,7 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {

fieldVal = eindirect(fieldVal)

if isNil(fieldVal) { // Don't write anything for nil fields.
if isNil(fieldVal) { /// Don't write anything for nil fields.
continue
}

26 changes: 11 additions & 15 deletions parse.go
Original file line number Diff line number Diff line change
@@ -11,10 +11,7 @@ import (
"github.com/BurntSushi/toml/internal"
)

var tomlNext = func() bool {
_, ok := os.LookupEnv("BURNTSUSHI_TOML_110")
return ok
}()
var tomlNext bool

type parser struct {
lx *lexer
@@ -35,6 +32,9 @@ type keyInfo struct {
}

func parse(data string) (p *parser, err error) {
_, ok := os.LookupEnv("BURNTSUSHI_TOML_110")
tomlNext = ok

defer func() {
if r := recover(); r != nil {
if pErr, ok := r.(ParseError); ok {
@@ -203,12 +203,12 @@ func (p *parser) topLevel(item item) {
for i := range context {
p.addImplicitContext(append(p.context, context[i:i+1]...))
}
p.ordered = append(p.ordered, p.context.add(p.currentKey))

/// Set value.
vItem := p.next()
val, typ := p.value(vItem, false)
p.set(p.currentKey, val, typ, vItem.pos)
p.ordered = append(p.ordered, p.context.add(p.currentKey))

/// Remove the context we added (preserving any context from [tbl] lines).
p.context = outerContext
@@ -445,11 +445,11 @@ func (p *parser) valueInlineTable(it item, parentIsArray bool) (interface{}, tom
for i := range context {
p.addImplicitContext(append(p.context, context[i:i+1]...))
}
p.ordered = append(p.ordered, p.context.add(p.currentKey))

/// Set the value.
val, typ := p.value(p.next(), false)
p.set(p.currentKey, val, typ, it.pos)
p.ordered = append(p.ordered, p.context.add(p.currentKey))
hash[p.currentKey] = val

/// Restore context.
@@ -570,7 +570,6 @@ func (p *parser) addContext(key Key, array bool) {
func (p *parser) set(key string, val interface{}, typ tomlType, pos Position) {
p.setValue(key, val)
p.setType(key, typ, pos)

}

// setValue sets the given key to the given value in the current context.
@@ -651,14 +650,11 @@ func (p *parser) setType(key string, typ tomlType, pos Position) {

// Implicit keys need to be created when tables are implied in "a.b.c.d = 1" and
// "[a.b.c]" (the "a", "b", and "c" hashes are never created explicitly).
func (p *parser) addImplicit(key Key) { p.implicits[key.String()] = struct{}{} }
func (p *parser) removeImplicit(key Key) { delete(p.implicits, key.String()) }
func (p *parser) isImplicit(key Key) bool { _, ok := p.implicits[key.String()]; return ok }
func (p *parser) isArray(key Key) bool { return p.keyInfo[key.String()].tomlType == tomlArray }
func (p *parser) addImplicitContext(key Key) {
p.addImplicit(key)
p.addContext(key, false)
}
func (p *parser) addImplicit(key Key) { p.implicits[key.String()] = struct{}{} }
func (p *parser) removeImplicit(key Key) { delete(p.implicits, key.String()) }
func (p *parser) isImplicit(key Key) bool { _, ok := p.implicits[key.String()]; return ok }
func (p *parser) isArray(key Key) bool { return p.keyInfo[key.String()].tomlType == tomlArray }
func (p *parser) addImplicitContext(key Key) { p.addImplicit(key); p.addContext(key, false) }

// current returns the full key name of the current context.
func (p *parser) current() string {