From b7d60a03d1acec90c8dd31ab41b8a5cdbc6b2e56 Mon Sep 17 00:00:00 2001
From: David Bloss <david@opslevel.com>
Date: Mon, 30 Dec 2024 15:22:06 -0600
Subject: [PATCH] add getFieldTypeForObject, used when generating input objects

---
 gen.go                     | 50 ++++++++++++++++++++++++++++++++++++--
 templates/inputObjects.tpl |  2 +-
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/gen.go b/gen.go
index 95d7e1a2..ed0bd77b 100644
--- a/gen.go
+++ b/gen.go
@@ -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
@@ -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"
 }
@@ -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,
diff --git a/templates/inputObjects.tpl b/templates/inputObjects.tpl
index acd907a8..7c0cc785 100644
--- a/templates/inputObjects.tpl
+++ b/templates/inputObjects.tpl
@@ -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 . }}