disable nested struct for array and map type (#4222)

This commit is contained in:
kesonan 2024-06-29 13:44:46 +08:00 committed by GitHub
parent 01bbc78bac
commit 2f9b6cf8ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 81 additions and 4 deletions

View File

@ -50,6 +50,10 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
} }
members = append(members, m) members = append(members, m)
} }
if v.RawText() == "{}" {
return nil, ast.SyntaxError(v.Pos(), "unsupported empty struct")
}
return spec.DefineStruct{ return spec.DefineStruct{
RawName: v.RawText(), RawName: v.RawText(),
Members: members, Members: members,
@ -58,10 +62,13 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
return spec.InterfaceType{RawName: v.RawText()}, nil return spec.InterfaceType{RawName: v.RawText()}, nil
case *ast.MapDataType: case *ast.MapDataType:
if !isLiteralType(v.Key) { 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() { 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) value, err := a.astTypeToSpec(v.Value)
@ -91,9 +98,11 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
}, nil }, nil
case *ast.ArrayDataType: case *ast.ArrayDataType:
if v.Length.Token.Type == token.ELLIPSIS { 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) value, err := a.astTypeToSpec(v.DataType)
if err != nil { if err != nil {
return nil, err return nil, err
@ -104,6 +113,10 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
Value: value, Value: value,
}, nil }, nil
case *ast.SliceDataType: case *ast.SliceDataType:
if v.ContainsStruct() {
return nil, ast.SyntaxError(v.Pos(), "slice elements unsupported nested struct")
}
value, err := a.astTypeToSpec(v.DataType) value, err := a.astTypeToSpec(v.DataType)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -123,6 +123,70 @@ type Foo {
Bar any `json:"bar"` 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 // test case: unresolved type
syntax = "v1" syntax = "v1"