Skip to content

Commit 3621b6d

Browse files
committed
x/exp/schema/internal/parser: make parser error when things are defined more than once
Signed-off-by: philhassey <phil@strongdm.com>
1 parent eac3029 commit 3621b6d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

x/exp/schema/internal/parser/parser.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ func (p *parser) parseSchema() (*ast.Schema, error) {
122122
if schema.Namespaces == nil {
123123
schema.Namespaces = ast.Namespaces{}
124124
}
125+
if _, ok := schema.Namespaces[ns.name]; ok {
126+
return nil, p.errorf("namespace %q is declared twice", ns.name)
127+
}
125128
schema.Namespaces[ns.name] = ns.ns
126129
} else {
127130
if err := p.parseDecl(annotations, schema); err != nil {
@@ -260,6 +263,12 @@ func (p *parser) parseEntity(annotations ast.Annotations, schema *ast.Schema) er
260263
schema.Entities = ast.Entities{}
261264
}
262265
for _, name := range names {
266+
if _, ok := schema.Entities[name]; ok {
267+
return p.errorf("entity %q is declared twice", name)
268+
}
269+
if _, ok := schema.Enums[name]; ok {
270+
return p.errorf("entity %q is declared twice", name)
271+
}
263272
schema.Entities[name] = ast.Entity{
264273
Annotations: annotations,
265274
ParentTypes: memberOf,
@@ -302,6 +311,12 @@ func (p *parser) parseEnumEntity(annotations ast.Annotations, names []types.Iden
302311
schema.Enums = ast.Enums{}
303312
}
304313
for _, name := range names {
314+
if _, ok := schema.Enums[name]; ok {
315+
return p.errorf("entity %q is declared twice", name)
316+
}
317+
if _, ok := schema.Entities[name]; ok {
318+
return p.errorf("entity %q is declared twice", name)
319+
}
305320
schema.Enums[name] = ast.Enum{
306321
Annotations: annotations,
307322
Values: values,
@@ -362,6 +377,9 @@ func (p *parser) parseAction(annotations ast.Annotations, schema *ast.Schema) er
362377
schema.Actions = ast.Actions{}
363378
}
364379
for _, name := range names {
380+
if _, ok := schema.Actions[name]; ok {
381+
return p.errorf("action %q is declared twice", name)
382+
}
365383
schema.Actions[name] = ast.Action{
366384
Annotations: annotations,
367385
Parents: memberOf,
@@ -396,7 +414,11 @@ func (p *parser) parseTypeDecl(annotations ast.Annotations, schema *ast.Schema)
396414
if schema.CommonTypes == nil {
397415
schema.CommonTypes = ast.CommonTypes{}
398416
}
399-
schema.CommonTypes[types.Ident(name)] = ast.CommonType{
417+
ident := types.Ident(name)
418+
if _, ok := schema.CommonTypes[ident]; ok {
419+
return p.errorf("type %q is declared twice", name)
420+
}
421+
schema.CommonTypes[ident] = ast.CommonType{
400422
Annotations: annotations,
401423
Type: typ,
402424
}

x/exp/schema/internal/parser/parser_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,3 +943,28 @@ func TestParseSchemaErrors(t *testing.T) {
943943
})
944944
}
945945
}
946+
947+
func TestParseDuplicateDeclarations(t *testing.T) {
948+
tests := []struct {
949+
name string
950+
input string
951+
}{
952+
{"duplicate entity", `entity User; entity User;`},
953+
{"duplicate entity in namespace", `namespace Foo { entity User; entity User; }`},
954+
{"duplicate enum", `entity Status enum ["a"]; entity Status enum ["b"];`},
955+
{"duplicate action", `action view; action view;`},
956+
{"duplicate action in namespace", `namespace Foo { action view; action view; }`},
957+
{"duplicate common type", `type Ctx = { x: Long }; type Ctx = { y: Long };`},
958+
{"duplicate namespace", "namespace Foo { entity A; }\nnamespace Foo { entity B; }"},
959+
{"entity conflicts with enum", `entity User; entity User enum ["a"];`},
960+
{"enum conflicts with entity", `entity User enum ["a"]; entity User;`},
961+
{"duplicate multi-name entity", `entity A, A { name: String };`},
962+
{"duplicate multi-name action", `action read, read;`},
963+
}
964+
for _, tt := range tests {
965+
t.Run(tt.name, func(t *testing.T) {
966+
_, err := parser.ParseSchema("", []byte(tt.input))
967+
testutil.Error(t, err)
968+
})
969+
}
970+
}

0 commit comments

Comments
 (0)