Skip to content

Commit

Permalink
FIX generics like type JSON[K,V string] map[K]V (#1509)
Browse files Browse the repository at this point in the history
Co-authored-by: hepei <[email protected]>
  • Loading branch information
thinkhp and thinkhp authored Mar 23, 2023
1 parent f475da2 commit 122a2e2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
6 changes: 6 additions & 0 deletions generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func (pkgDefs *PackagesDefinitions) resolveGenericType(file *ast.File, expr ast.
Len: astExpr.Len,
Lbrack: astExpr.Lbrack,
}
case *ast.MapType:
return &ast.MapType{
Map: astExpr.Map,
Key: pkgDefs.resolveGenericType(file, astExpr.Key, genericParamTypeDefs),
Value: pkgDefs.resolveGenericType(file, astExpr.Value, genericParamTypeDefs),
}
case *ast.StarExpr:
return &ast.StarExpr{
Star: astExpr.Star,
Expand Down
47 changes: 47 additions & 0 deletions testdata/generics_basic/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@
"types.Hello": {
"type": "object",
"properties": {
"arrayField": {
"type": "array",
"items": {
"type": "string"
}
},
"mapField": {
"$ref": "#/definitions/types.MapField-string-float64"
},
"mapFieldNestedStruct": {
"$ref": "#/definitions/types.MapFieldNestedStruct-string"
},
"myNewArrayField": {
"type": "array",
"items": {
Expand All @@ -297,6 +309,41 @@
},
"myStringField2": {
"type": "string"
},
"originArrayField": {
"type": "array",
"items": {
"type": "string"
}
},
"originMapField": {
"type": "object",
"additionalProperties": {
"type": "number"
}
}
}
},
"types.MapField-string-float64": {
"type": "object",
"additionalProperties": {
"type": "number"
}
},
"types.MapFieldNestedStruct-string": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/types.MapFieldValue"
}
},
"types.MapFieldValue": {
"type": "object",
"properties": {
"f": {
"type": "number"
},
"s": {
"type": "string"
}
}
},
Expand Down
21 changes: 17 additions & 4 deletions testdata/generics_basic/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ type TrippleField[T1 any, T2 any] struct {
Value2 T2
}

type ArrayField[T any] []T
type MapField[K comparable, V any] map[K]V
type MapFieldValue struct {
S string
F float64
}
type MapFieldNestedStruct[K comparable] map[K]MapFieldValue

type Hello struct {
MyStringField1 Field[*string] `json:"myStringField1"`
MyStringField2 Field[string] `json:"myStringField2"`
MyArrayField DoubleField[*string, string] `json:"myNewField"`
MyArrayDepthField TrippleField[*string, string] `json:"myNewArrayField"`
MyStringField1 Field[*string] `json:"myStringField1"`
MyStringField2 Field[string] `json:"myStringField2"`
MyArrayField DoubleField[*string, string] `json:"myNewField"`
MyArrayDepthField TrippleField[*string, string] `json:"myNewArrayField"`
ArrayField ArrayField[string] `json:"arrayField"`
MapField MapField[string, float64] `json:"mapField"`
OriginArrayField []string `json:"originArrayField"`
OriginMapField map[string]float64 `json:"originMapField"`
MapFieldNestedStruct MapFieldNestedStruct[string] `json:"mapFieldNestedStruct"`
}

0 comments on commit 122a2e2

Please sign in to comment.