Skip to content

Commit

Permalink
Merge pull request #36 from nukleros/fix-string-api-resources
Browse files Browse the repository at this point in the history
fix: use strconv package instead of rune/string
  • Loading branch information
lander2k2 authored Jun 15, 2022
2 parents 8fbdd79 + 8e5a7da commit 6bb5232
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Definition struct {
// input fields
Builder kinds.WorkloadBuilder
Manifest *manifests.Manifest

// template fields
UseStrConv bool
}

func (f *Definition) SetTemplateDefaults() error {
Expand All @@ -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

Expand All @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions internal/workload/v1/kinds/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions internal/workload/v1/manifests/child_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ChildResource struct {
StaticContent string
SourceCode string
IncludeCode string
UseStrConv bool
RBAC *rbac.Rules
}

Expand Down
4 changes: 3 additions & 1 deletion internal/workload/v1/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 6bb5232

Please sign in to comment.