fix: use null and extend on types

This commit is contained in:
Kevin Franklin Kim 2022-02-16 16:11:50 +01:00
parent f64c4e12e8
commit 1e249e4602
2 changed files with 29 additions and 15 deletions

View File

@ -34,11 +34,16 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S
ts.app(",") ts.app(",")
v.Map.Value.tsType(mappings, scalars, structs, ts) v.Map.Value.tsType(mappings, scalars, structs, ts)
ts.app(">") ts.app(">")
ts.app("|null")
case v.Array != nil: case v.Array != nil:
if v.Array.Value.ScalarType != ScalarTypeByte {
ts.app("Array<")
}
v.Array.Value.tsType(mappings, scalars, structs, ts) v.Array.Value.tsType(mappings, scalars, structs, ts)
if v.Array.Value.ScalarType != ScalarTypeByte { if v.Array.Value.ScalarType != ScalarTypeByte {
ts.app("[]") ts.app(">")
} }
ts.app("|null")
case v.Scalar != nil: case v.Scalar != nil:
if v.Scalar.Package != "" { if v.Scalar.Package != "" {
mapping, ok := mappings[v.Scalar.Package] mapping, ok := mappings[v.Scalar.Package]
@ -89,6 +94,9 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S
} }
ts.app(tsModule + "." + v.StructType.Name) ts.app(tsModule + "." + v.StructType.Name)
if v.IsPtr {
ts.app("|null")
}
return return
} }
ts.app(v.StructType.Name) ts.app(v.StructType.Name)
@ -97,10 +105,19 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S
ts.l("{").ind(1) ts.l("{").ind(1)
renderStructFields(v.Struct.Fields, mappings, scalars, structs, ts) renderStructFields(v.Struct.Fields, mappings, scalars, structs, ts)
ts.ind(-1).app("}") ts.ind(-1).app("}")
if v.IsPtr {
ts.app("|null")
}
case len(v.ScalarType) > 0: case len(v.ScalarType) > 0:
ts.app(tsTypeFromScalarType(v.ScalarType)) ts.app(tsTypeFromScalarType(v.ScalarType))
if v.IsPtr {
ts.app("|null")
}
default: default:
ts.app("any") ts.app("any")
if v.IsPtr {
ts.app("|null")
}
} }
return return
} }
@ -121,10 +138,7 @@ func renderStructFields(fields []*Field, mappings config.TypeScriptMappings, sca
continue continue
} }
ts.app(f.tsName()) ts.app(f.tsName())
if f.Value.IsPtr || if f.JSONInfo != nil && f.JSONInfo.OmitEmpty {
f.Value.Map != nil ||
f.Value.Array != nil ||
(f.JSONInfo != nil && f.JSONInfo.OmitEmpty) {
ts.app("?") ts.app("?")
} }
ts.app(":") ts.app(":")

View File

@ -48,14 +48,14 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap
ts.app(", ") ts.app(", ")
} }
ts.app(arg.tsName()) ts.app(arg.tsName())
ts.app(":")
arg.Value.tsType(mappings, scalars, structs, ts)
if arg.Value.IsPtr || if arg.Value.IsPtr ||
arg.Value.Map != nil || arg.Value.Map != nil ||
arg.Value.Array != nil || arg.Value.Array != nil ||
(arg.JSONInfo != nil && arg.JSONInfo.OmitEmpty) { (arg.JSONInfo != nil && arg.JSONInfo.OmitEmpty) {
ts.app("?") ts.app("|null")
} }
ts.app(":")
arg.Value.tsType(mappings, scalars, structs, ts)
callArgs = append(callArgs, arg.Name) callArgs = append(callArgs, arg.Name)
argCount++ argCount++
} }
@ -97,11 +97,11 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap
} }
innerReturnTypeTS.app(strconv.Itoa(index)) innerReturnTypeTS.app(strconv.Itoa(index))
if isNilable {
innerReturnTypeTS.app("?")
}
innerReturnTypeTS.app(":") innerReturnTypeTS.app(":")
retField.Value.tsType(mappings, scalars, structs, innerReturnTypeTS) retField.Value.tsType(mappings, scalars, structs, innerReturnTypeTS)
if isNilable {
innerReturnTypeTS.app("|null")
}
if index == len(method.Return)-1 && retField.Value.IsError { if index == len(method.Return)-1 && retField.Value.IsError {
throwLastError = true throwLastError = true
@ -113,18 +113,18 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap
retField.Value.tsType(mappings, scalars, structs, firstReturnTypeTS) retField.Value.tsType(mappings, scalars, structs, firstReturnTypeTS)
firstReturnType = firstReturnTypeTS.string() firstReturnType = firstReturnTypeTS.string()
if isNilable { if isNilable {
firstReturnType += "|undefined" firstReturnType += "|null"
} }
//firstReturnFieldName = retArgName //firstReturnFieldName = retArgName
} }
countReturns++ countReturns++
returnTypeTS.app(retArgName) returnTypeTS.app(retArgName)
if isNilable {
returnTypeTS.app("?")
}
returnTypeTS.app(":") returnTypeTS.app(":")
responseObject += responseObjectPrefix + retArgName + " : response[" + strconv.Itoa(index) + "]" responseObject += responseObjectPrefix + retArgName + " : response[" + strconv.Itoa(index) + "]"
retField.Value.tsType(mappings, scalars, structs, returnTypeTS) retField.Value.tsType(mappings, scalars, structs, returnTypeTS)
if isNilable {
returnTypeTS.app("|null")
}
} }
responseObjectPrefix = ", " responseObjectPrefix = ", "
} }