Skip to content

Commit

Permalink
disable nested struct for array and map type (#4222)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan authored Jun 29, 2024
1 parent 01bbc78 commit 2f9b6cf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tools/goctl/pkg/parser/api/parser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
64 changes: 64 additions & 0 deletions tools/goctl/pkg/parser/api/parser/testdata/invalid.api
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2f9b6cf

Please sign in to comment.