diff --git a/ast/ast_test.go b/ast/ast_test.go index 0365ebe..a2718cf 100644 --- a/ast/ast_test.go +++ b/ast/ast_test.go @@ -254,8 +254,8 @@ func testAST(t *testing.T, code func() (error, *ast.Document), expectedError str func Test_NewArray(t *testing.T) { a := ast.NewArray() - if a.Type != "" { - t.Fatalf("New array unexpectedly has a Type set to %q", a.Type) + if a.ItemType != "" { + t.Fatalf("New array unexpectedly has a Type set to %q", a.ItemType) } if a.First != nil { t.Fatalf("New array unexpectedly has the First item set") diff --git a/parse/keyvaluepair.go b/parse/keyvaluepair.go index 9f907a1..a2912de 100644 --- a/parse/keyvaluepair.go +++ b/parse/keyvaluepair.go @@ -2,6 +2,7 @@ package parse import ( "git.makaay.nl/mauricem/go-parsekit/parse" + "git.makaay.nl/mauricem/go-toml/ast" ) // The primary building block of a TOML document is the table. @@ -35,7 +36,7 @@ var ( ) func (t *parser) startKeyValuePair(p *parse.API) { - key, ok := t.parseKey(p, []string{}) + key, ok := t.parseKey(p, ast.NewKey()) if ok && p.Handle(t.startAssignment) { if value, ok := t.parseValue(p); ok { err := t.doc.SetKeyValuePair(key, value) @@ -57,7 +58,7 @@ func (t *parser) startKeyValuePair(p *parse.API) { // is to use bare keys except when absolutely necessary. // A bare key must be non-empty, but an empty quoted key is allowed (though // discouraged). -func (t *parser) parseKey(p *parse.API, key []string) ([]string, bool) { +func (t *parser) parseKey(p *parse.API, key ast.Key) (ast.Key, bool) { var keyPart string var ok bool switch { @@ -82,7 +83,7 @@ func (t *parser) parseKey(p *parse.API, key []string) ([]string, bool) { // This allows for grouping similar properties together. // Whitespace around dot-separated parts is ignored, however, best // practice is to not use any extraneous whitespace. -func (t *parser) parseEndOfKeyOrDot(p *parse.API, key []string) ([]string, bool) { +func (t *parser) parseEndOfKeyOrDot(p *parse.API, key ast.Key) (ast.Key, bool) { if p.Accept(keySeparatorDot) { return t.parseKey(p, key) } diff --git a/parse/parse.go b/parse/parse.go index c730f13..fd04ff8 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -58,3 +58,11 @@ func Run(input interface{}) (ast.Table, error) { err := parse.New(p.startDocument)(input) return p.doc.Root, err } + +// RunWithoutSanityChecks runs the TOML parser against the provided input data. +// The parsekit sanity checks are disabled during the parse run. +func RunWithoutSanityChecks(input interface{}) (ast.Table, error) { + p := newParser() + err := parse.NewWithoutSanityChecks(p.startDocument)(input) + return p.doc.Root, err +} diff --git a/parse/testfiles_test.go b/parse/testfiles_test.go index fd913f9..291be91 100644 --- a/parse/testfiles_test.go +++ b/parse/testfiles_test.go @@ -14,6 +14,7 @@ import ( "time" "git.makaay.nl/mauricem/go-toml/parse" + "github.com/pkg/profile" ) func Test_Invalid(t *testing.T) { @@ -37,6 +38,7 @@ func Test_Invalid(t *testing.T) { } func Test_Valid(t *testing.T) { + defer profile.Start().Stop() for _, suite := range getTestSuites("valid") { success := 0 fail := 0 @@ -45,7 +47,7 @@ func Test_Valid(t *testing.T) { if err != nil { panic(fmt.Sprintf("Cannot open toml file for test (%v): %s", testCase, err)) } - tomlTable, err := parse.Run(input) + tomlTable, err := parse.RunWithoutSanityChecks(input) if err != nil { t.Errorf("[%s] parse failed unexpectedly: %s", name, err) fail++ @@ -160,13 +162,15 @@ func cmpJsonObjects(key string, expected map[string]interface{}, a interface{}) actual, _ := a.(map[string]interface{}) // Check to make sure both or neither are values. - if isValue(expected) && !isValue(actual) { + eIsVal := isValue(expected) + aIsVal := isValue(actual) + if eIsVal && !aIsVal { return fmt.Errorf("Key '%s' is supposed to be a value, but the parser reports it as a table.", key) } - if !isValue(expected) && isValue(actual) { + if !eIsVal && aIsVal { return fmt.Errorf("Key '%s' is supposed to be a table, but the parser reports it as a value.", key) } - if isValue(expected) && isValue(actual) { + if eIsVal && aIsVal { return cmpJsonValues(key, expected, actual) }