diff --git a/internal/plugins/workload/v1/scaffolds/templates/api/resources/definition.go b/internal/plugins/workload/v1/scaffolds/templates/api/resources/definition.go index 7fe4398..21d2c7e 100644 --- a/internal/plugins/workload/v1/scaffolds/templates/api/resources/definition.go +++ b/internal/plugins/workload/v1/scaffolds/templates/api/resources/definition.go @@ -24,6 +24,9 @@ type Definition struct { // input fields Builder kinds.WorkloadBuilder Manifest *manifests.Manifest + + // template fields + UseStrConv bool } func (f *Definition) SetTemplateDefaults() error { @@ -35,6 +38,15 @@ func (f *Definition) SetTemplateDefaults() error { f.Manifest.SourceFilename, ) + // determine if we need to import the strconv package + for _, child := range f.Manifest.ChildResources { + if child.UseStrConv { + f.UseStrConv = true + + break + } + } + f.TemplateBody = definitionTemplate f.IfExistsAction = machinery.OverwriteFile @@ -47,6 +59,8 @@ const definitionTemplate = `{{ .Boilerplate }} package {{ .Builder.GetPackageName }} import ( + {{ if .UseStrConv }}"strconv"{{ end }} + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" diff --git a/internal/workload/v1/kinds/workload.go b/internal/workload/v1/kinds/workload.go index 9e488d7..c580fc4 100644 --- a/internal/workload/v1/kinds/workload.go +++ b/internal/workload/v1/kinds/workload.go @@ -278,6 +278,12 @@ func (ws *WorkloadSpec) processManifests(markerTypes ...markers.MarkerType) erro childResource.SourceCode = resourceDefinition childResource.StaticContent = manifest + // HACK: we should handle this better, for now this will work. we are passing info along that one of our + // resources needs to use the strconv package and needs to be included in the generated code. + if strings.Contains(resourceDefinition, "strconv.Itoa") || strings.Contains(resourceDefinition, "strconv.FormatBool") { + childResource.UseStrConv = true + } + childResources = append(childResources, *childResource) } diff --git a/internal/workload/v1/manifests/child_resource.go b/internal/workload/v1/manifests/child_resource.go index 87f6d4d..0fb94fd 100644 --- a/internal/workload/v1/manifests/child_resource.go +++ b/internal/workload/v1/manifests/child_resource.go @@ -34,6 +34,7 @@ type ChildResource struct { StaticContent string SourceCode string IncludeCode string + UseStrConv bool RBAC *rbac.Rules } diff --git a/internal/workload/v1/markers/markers.go b/internal/workload/v1/markers/markers.go index 7efa8fe..b4159cb 100644 --- a/internal/workload/v1/markers/markers.go +++ b/internal/workload/v1/markers/markers.go @@ -224,7 +224,9 @@ func getSourceCodeFieldVariable(marker FieldMarkerProcessor) (string, error) { case FieldString: return fmt.Sprintf("!!start %s !!end", marker.GetSourceCodeVariable()), nil case FieldInt: - return fmt.Sprintf("!!start string(rune(%s)) !!end", marker.GetSourceCodeVariable()), nil + return fmt.Sprintf("!!start strconv.Itoa(%s) !!end", marker.GetSourceCodeVariable()), nil + case FieldBool: + return fmt.Sprintf("!!start strconv.FormatBool(%s) !!end", marker.GetSourceCodeVariable()), nil default: return "", fmt.Errorf("%w with field type %s", ErrInvalidReplaceMarkerFieldType, marker.GetFieldType()) }