Skip to content

Commit 60801d0

Browse files
committed
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
1 parent c859a22 commit 60801d0

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

Diff for: decode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
248248
case reflect.Bool:
249249
return md.unifyBool(data, rv)
250250
case reflect.Interface:
251-
if rv.NumMethod() > 0 { // Only support empty interfaces are supported.
251+
if rv.NumMethod() > 0 { /// Only empty interfaces are supported.
252252
return md.e("unsupported type %s", rv.Type())
253253
}
254254
return md.unifyAnything(data, rv)

Diff for: decode_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,36 @@ func TestDecodeDoubleTags(t *testing.T) {
11711171
}
11721172
}
11731173

1174+
func TestMetaKeys(t *testing.T) {
1175+
tests := []struct {
1176+
in string
1177+
want []Key
1178+
}{
1179+
{"", []Key{}},
1180+
{"b=1\na=1", []Key{Key{"b"}, Key{"a"}}},
1181+
{"a.b=1\na.a=1", []Key{Key{"a", "b"}, Key{"a", "a"}}}, // TODO: should include "a"
1182+
{"[tbl]\na=1", []Key{Key{"tbl"}, Key{"tbl", "a"}}},
1183+
{"[tbl]\na.a=1", []Key{Key{"tbl"}, Key{"tbl", "a", "a"}}}, // TODO: should include "a.a"
1184+
{"tbl={a=1}", []Key{Key{"tbl"}, Key{"tbl", "a"}}},
1185+
{"tbl={a={b=1}}", []Key{Key{"tbl"}, Key{"tbl", "a"}, Key{"tbl", "a", "b"}}},
1186+
}
1187+
1188+
for _, tt := range tests {
1189+
t.Run("", func(t *testing.T) {
1190+
var x interface{}
1191+
meta, err := Decode(tt.in, &x)
1192+
if err != nil {
1193+
t.Fatal(err)
1194+
}
1195+
1196+
have := meta.Keys()
1197+
if !reflect.DeepEqual(tt.want, have) {
1198+
t.Errorf("\nhave: %s\nwant: %s\n", have, tt.want)
1199+
}
1200+
})
1201+
}
1202+
}
1203+
11741204
// errorContains checks if the error message in have contains the text in
11751205
// want.
11761206
//

Diff for: encode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
503503

504504
fieldVal = eindirect(fieldVal)
505505

506-
if isNil(fieldVal) { // Don't write anything for nil fields.
506+
if isNil(fieldVal) { /// Don't write anything for nil fields.
507507
continue
508508
}
509509

Diff for: parse.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ func (p *parser) topLevel(item item) {
203203
for i := range context {
204204
p.addImplicitContext(append(p.context, context[i:i+1]...))
205205
}
206+
p.ordered = append(p.ordered, p.context.add(p.currentKey))
206207

207208
/// Set value.
208209
vItem := p.next()
209210
val, typ := p.value(vItem, false)
210211
p.set(p.currentKey, val, typ, vItem.pos)
211-
p.ordered = append(p.ordered, p.context.add(p.currentKey))
212212

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

449450
/// Set the value.
450451
val, typ := p.value(p.next(), false)
451452
p.set(p.currentKey, val, typ, it.pos)
452-
p.ordered = append(p.ordered, p.context.add(p.currentKey))
453453
hash[p.currentKey] = val
454454

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

576575
// 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) {
651650

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

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

0 commit comments

Comments
 (0)