Forbid adding keys to exist inline table (#378)
This commit is contained in:
@@ -158,6 +158,11 @@ func (p *tomlParser) parseGroup() tomlParserStateFn {
|
|||||||
if err := p.tree.createSubTree(keys, startToken.Position); err != nil {
|
if err := p.tree.createSubTree(keys, startToken.Position); err != nil {
|
||||||
p.raiseError(key, "%s", err)
|
p.raiseError(key, "%s", err)
|
||||||
}
|
}
|
||||||
|
destTree := p.tree.GetPath(keys)
|
||||||
|
if target, ok := destTree.(*Tree); ok && target != nil && target.inline {
|
||||||
|
p.raiseError(key, "could not re-define exist inline table or its sub-table : %s",
|
||||||
|
strings.Join(keys, "."))
|
||||||
|
}
|
||||||
p.assume(tokenRightBracket)
|
p.assume(tokenRightBracket)
|
||||||
p.currentTable = keys
|
p.currentTable = keys
|
||||||
return p.parseStart
|
return p.parseStart
|
||||||
@@ -201,6 +206,11 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
|
|||||||
strings.Join(tableKey, "."))
|
strings.Join(tableKey, "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if targetNode.inline {
|
||||||
|
p.raiseError(key, "could not add key or sub-table to exist inline table or its sub-table : %s",
|
||||||
|
strings.Join(tableKey, "."))
|
||||||
|
}
|
||||||
|
|
||||||
// assign value to the found table
|
// assign value to the found table
|
||||||
keyVal := parsedKey[len(parsedKey)-1]
|
keyVal := parsedKey[len(parsedKey)-1]
|
||||||
localKey := []string{keyVal}
|
localKey := []string{keyVal}
|
||||||
@@ -411,6 +421,7 @@ Loop:
|
|||||||
if tokenIsComma(previous) {
|
if tokenIsComma(previous) {
|
||||||
p.raiseError(previous, "trailing comma at the end of inline table")
|
p.raiseError(previous, "trailing comma at the end of inline table")
|
||||||
}
|
}
|
||||||
|
tree.inline = true
|
||||||
return tree
|
return tree
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -703,6 +703,34 @@ func TestInlineTableTrailingComma(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddKeyToInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("type = { name = \"Nail\" }\ntype.edible = false")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : type" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddSubTableToInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = \"c\" }\na.d.e = \"f\"")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : a.d" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddKeyToSubTableOfInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = { c = \"d\" } }\na.b.e = \"f\"")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : a.b" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReDefineInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = \"c\" }\n[a]\n d = \"e\"")
|
||||||
|
if err.Error() != "(2, 2): could not re-define exist inline table or its sub-table : a" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDuplicateGroups(t *testing.T) {
|
func TestDuplicateGroups(t *testing.T) {
|
||||||
_, err := Load("[foo]\na=2\n[foo]b=3")
|
_, err := Load("[foo]\na=2\n[foo]b=3")
|
||||||
if err.Error() != "(3, 2): duplicated tables" {
|
if err.Error() != "(3, 2): duplicated tables" {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type Tree struct {
|
|||||||
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
|
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
|
||||||
comment string
|
comment string
|
||||||
commented bool
|
commented bool
|
||||||
|
inline bool
|
||||||
position Position
|
position Position
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,6 +312,7 @@ func (t *Tree) createSubTree(keys []string, pos Position) error {
|
|||||||
if !exists {
|
if !exists {
|
||||||
tree := newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col})
|
tree := newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col})
|
||||||
tree.position = pos
|
tree.position = pos
|
||||||
|
tree.inline = subtree.inline
|
||||||
subtree.values[intermediateKey] = tree
|
subtree.values[intermediateKey] = tree
|
||||||
nextTree = tree
|
nextTree = tree
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user