Skip to content

Commit

Permalink
add getFieldTypeForObject, used when generating input objects
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Dec 30, 2024
1 parent 0d83852 commit b7d60a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
50 changes: 48 additions & 2 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,13 @@ func graphqlTypeToGolang(graphqlType string) string {
switch graphqlType {
case "Boolean":
convertedType += "bool"
case "Float":
convertedType += "float64"
case "Int":
convertedType += "int"
case "ISO8601DateTime":
convertedType += "iso8601.Time"
case "Float", "String":
case "String":
convertedType += "string"
default:
convertedType += graphqlType
Expand All @@ -1105,6 +1107,50 @@ func getFieldTypeNew(fieldType types.Type) string {
return graphqlTypeToGolang(fieldType.String())
}

func getFieldTypeForObject(fieldType types.FieldDefinition) string {
goType := graphqlTypeToGolang(fieldType.Type.String())
if strings.HasPrefix(goType, "*Nullable[") {
goType = strings.TrimPrefix(goType, "*Nullable[")
goType = strings.TrimSuffix(goType, "]")
}

switch goType {
case "Filter":
return "FilterId"
case "JSONSchema":
return "JSON"
case "Service":
return "ServiceId"
case "Team":
goType = "TeamId"
case "User":
return "UserId"
}

if fieldType.Name == "AlertSource" && goType == "Integration" {
goType = "IntegrationId"
} else if fieldType.Name == "CustomActionsTriggerDefinition" && goType == "Action" {
goType = "CustomActionsId"
} else if fieldType.Name == "FilterPredicate" && goType == "CaseSensitive" {
goType = "*bool"
} else if fieldType.Name == "Property" {
switch goType {
case "[]Error":
goType = "[]OpsLevelErrors"
case "HasProperties":
goType = "EntityOwnerService"
case "JsonString":
goType = "*JsonString"
case "PropertyDefinition":
goType = "PropertyDefinitionId"
}
} else if fieldType.Name == "ServiceRepository" && goType == "Repository" {
goType = "RepositoryId"
}

return goType
}

func isNullable(fieldType types.Type) bool {
return fieldType.Kind() != "NON_NULL"
}
Expand Down Expand Up @@ -1193,7 +1239,7 @@ var templFuncMap = template.FuncMap{
"skip_query": skipQuery,
"skip_interface_field": skipInterfaceField,
"isListType": isPlural,
"getFieldType": getFieldTypeNew,
"getFieldTypeForInputObject": getFieldTypeNew,
"exampleStructTag": exampleStructTag,
"jsonStructTag": jsonStructTag,
"yamlStructTag": yamlStructTag,
Expand Down
2 changes: 1 addition & 1 deletion templates/inputObjects.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
type {{ .Name }} struct { {{ range .Values }}
{{ title .Name.Name }} {{ if and (eq $.Name "AliasCreateInput") (eq .Name.Name "ownerId") }}ID
{{- else if and (eq $.Name "CheckPackageVersionUpdateInput") (eq .Name.Name "versionConstraintPredicate") }}*Nullable[PredicateUpdateInput]
{{- else }}{{ getFieldType .Type }}
{{- else }}{{ getFieldTypeForInputObject .Type }}
{{- end }} `
{{- jsonStructTag . }} {{ yamlStructTag . }}
{{- exampleStructTag . }}` {{ fieldCommentDescription . }}
Expand Down

0 comments on commit b7d60a0

Please sign in to comment.