Skip to content

Commit 564da95

Browse files
Expose SetSDKForStruct and SetResourceForStruct in templates (#140)
Description of changes: Makes `setSDKForStruct` and `setResourceForStruct` public, so that they are accessible for other parts of the generator. Exposes `SetSDKForStruct` as `GoCodeSetSDKForStruct` and `SetResourceForStruct` as `GoCodeSetResourceForStruct` in the generator, and includes `SDKAPI` as a template variable to allow querying of API shapes to use with parameters in the new template methods. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent cf45416 commit 564da95

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

pkg/generate/ack/apis.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func APIs(
8484
crdFileName := strcase.ToSnake(crd.Kind) + ".go"
8585
crdVars := &templateCRDVars{
8686
metaVars,
87+
m.SDKAPI,
8788
crd,
8889
}
8990
if err = ts.Add(crdFileName, "apis/crd.go.tpl", crdVars); err != nil {
@@ -105,5 +106,6 @@ type templateAPIVars struct {
105106
// code for a single top-level resource's API definition
106107
type templateCRDVars struct {
107108
templateset.MetaVars
108-
CRD *ackmodel.CRD
109+
SDKAPI *ackmodel.SDKAPI
110+
CRD *ackmodel.CRD
109111
}

pkg/generate/ack/controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
2323
"github.com/aws-controllers-k8s/code-generator/pkg/generate/templateset"
2424
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
25+
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
2526
)
2627

2728
var (
@@ -93,6 +94,12 @@ var (
9394
"GoCodeSetDeleteInput": func(r *ackmodel.CRD, sourceVarName string, targetVarName string, indentLevel int) string {
9495
return code.SetSDK(r.Config(), r, ackmodel.OpTypeDelete, sourceVarName, targetVarName, indentLevel)
9596
},
97+
"GoCodeSetSDKForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceFieldPath string, sourceVarName string, indentLevel int) string {
98+
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, indentLevel)
99+
},
100+
"GoCodeSetResourceForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceVarName string, sourceShapeRef *awssdkmodel.ShapeRef, indentLevel int) string {
101+
return code.SetResourceForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceVarName, sourceShapeRef, indentLevel)
102+
},
96103
"GoCodeCompare": func(r *ackmodel.CRD, deltaVarName string, sourceVarName string, targetVarName string, indentLevel int) string {
97104
return code.CompareResource(r.Config(), r, deltaVarName, sourceVarName, targetVarName, indentLevel)
98105
},
@@ -132,6 +139,7 @@ func Controller(
132139
controllerFuncMap["Hook"] = func(r *ackmodel.CRD, hookID string) string {
133140
crdVars := &templateCRDVars{
134141
metaVars,
142+
m.SDKAPI,
135143
r,
136144
}
137145
code, err := ResourceHookCode(templateBasePaths, r, hookID, crdVars, controllerFuncMap)
@@ -165,6 +173,7 @@ func Controller(
165173
tplPath := filepath.Join("pkg/resource", target)
166174
crdVars := &templateCRDVars{
167175
metaVars,
176+
m.SDKAPI,
168177
crd,
169178
}
170179
if err = ts.Add(outPath, tplPath, crdVars); err != nil {

pkg/generate/code/set_resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func setResourceForContainer(
990990
) string {
991991
switch sourceShapeRef.Shape.Type {
992992
case "structure":
993-
return setResourceForStruct(
993+
return SetResourceForStruct(
994994
cfg, r,
995995
targetFieldName,
996996
targetVarName,
@@ -1031,9 +1031,9 @@ func setResourceForContainer(
10311031
}
10321032
}
10331033

1034-
// setResourceForStruct returns a string of Go code that sets a target variable
1034+
// SetResourceForStruct returns a string of Go code that sets a target variable
10351035
// value to a source variable when the type of the source variable is a struct.
1036-
func setResourceForStruct(
1036+
func SetResourceForStruct(
10371037
cfg *ackgenconfig.Config,
10381038
r *model.CRD,
10391039
// The name of the CR field we're outputting for

pkg/generate/code/set_sdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func setSDKForContainer(
739739
) string {
740740
switch targetShapeRef.Shape.Type {
741741
case "structure":
742-
return setSDKForStruct(
742+
return SetSDKForStruct(
743743
cfg, r,
744744
targetFieldName,
745745
targetVarName,
@@ -874,9 +874,9 @@ func setSDKForSecret(
874874
return out
875875
}
876876

877-
// setSDKForStruct returns a string of Go code that sets a target variable
877+
// SetSDKForStruct returns a string of Go code that sets a target variable
878878
// value to a source variable when the type of the source variable is a struct.
879-
func setSDKForStruct(
879+
func SetSDKForStruct(
880880
cfg *ackgenconfig.Config,
881881
r *model.CRD,
882882
// The name of the CR field we're outputting for

0 commit comments

Comments
 (0)