diff --git a/tools/goctl/pkg/parser/api/parser/analyzer.go b/tools/goctl/pkg/parser/api/parser/analyzer.go index da189450..b47e7039 100644 --- a/tools/goctl/pkg/parser/api/parser/analyzer.go +++ b/tools/goctl/pkg/parser/api/parser/analyzer.go @@ -50,6 +50,10 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) { } members = append(members, m) } + if v.RawText() == "{}" { + return nil, ast.SyntaxError(v.Pos(), "unsupported empty struct") + } + return spec.DefineStruct{ RawName: v.RawText(), Members: members, @@ -58,10 +62,13 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) { return spec.InterfaceType{RawName: v.RawText()}, nil case *ast.MapDataType: if !isLiteralType(v.Key) { - return nil, ast.SyntaxError(v.Pos(), "expected literal type, got <%T>", v) + return nil, ast.SyntaxError(v.Pos(), "expected literal type, got <%T>", v.Key) } if !v.Key.CanEqual() { - return nil, ast.SyntaxError(v.Pos(), "map key <%T> must be equal data type", v) + return nil, ast.SyntaxError(v.Pos(), "map key <%T> must be equal data type", v.Key) + } + if v.Value.ContainsStruct() { + return nil, ast.SyntaxError(v.Pos(), "map value unsupported nested struct") } value, err := a.astTypeToSpec(v.Value) @@ -91,9 +98,11 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) { }, nil case *ast.ArrayDataType: if v.Length.Token.Type == token.ELLIPSIS { - return nil, ast.SyntaxError(v.Pos(), "Array: unsupported dynamic length") + return nil, ast.SyntaxError(v.Pos(), "array length unsupported dynamic length") + } + if v.ContainsStruct() { + return nil, ast.SyntaxError(v.Pos(), "array elements unsupported nested struct") } - value, err := a.astTypeToSpec(v.DataType) if err != nil { return nil, err @@ -104,6 +113,10 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) { Value: value, }, nil case *ast.SliceDataType: + if v.ContainsStruct() { + return nil, ast.SyntaxError(v.Pos(), "slice elements unsupported nested struct") + } + value, err := a.astTypeToSpec(v.DataType) if err != nil { return nil, err diff --git a/tools/goctl/pkg/parser/api/parser/testdata/invalid.api b/tools/goctl/pkg/parser/api/parser/testdata/invalid.api index cf58be84..db57c6f4 100644 --- a/tools/goctl/pkg/parser/api/parser/testdata/invalid.api +++ b/tools/goctl/pkg/parser/api/parser/testdata/invalid.api @@ -123,6 +123,70 @@ type Foo { Bar any `json:"bar"` } +----- +// test case: invalid map key +syntax = "v1" +type Foo { + M map[{}]string `json:"m"` +} + +----- +// test case: invalid map key +syntax = "v1" +type Foo { +M map[{ + Foo string `json:"foo"` +}]string `json:"m"` +} + +----- +// test case: invalid map value +syntax = "v1" +type Foo { +M map[int]{} `json:"m"` +} + +----- +// test case: invalid map value +syntax = "v1" +type Foo { +M map[int]{ + Foo int `json:"foo"` +} `json:"m"` +} + +----- +// test case: invalid array element +syntax = "v1" +type Foo { + Array [3]{} `json:"array"` +} + +----- +// test case: invalid array element +syntax = "v1" +type Foo { +Array [3]{ +Foo int `json:"foo"` +} `json:"array"` +} + +----- +// test case: invalid array element +syntax = "v1" +type Foo { +Array []{} `json:"array"` +} + +----- +// test case: invalid slice element +syntax = "v1" +type Foo { +Array []{ +Foo int `json:"foo"` +} `json:"array"` +} + ----- // test case: unresolved type syntax = "v1"