|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package code |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/aws-controllers-k8s/code-generator/pkg/fieldpath" |
| 21 | + |
| 22 | + "github.com/aws-controllers-k8s/code-generator/pkg/model" |
| 23 | +) |
| 24 | + |
| 25 | +// InitializeNestedStructField returns the go code for initializing a nested |
| 26 | +// struct field. Currently this method only supports the struct shape for |
| 27 | +// nested elements. |
| 28 | +// |
| 29 | +// TODO(vijtrip2): Refactor the code out of set_resource.go for generating |
| 30 | +// constructors and reuse here. This method is currently being used for handling |
| 31 | +// nested Tagging fields. |
| 32 | +// |
| 33 | +// Example: generated code for "Logging.LoggingEnabled.TargetBucket" field |
| 34 | +// inside "s3" "bucket" crd looks like: |
| 35 | +// |
| 36 | +// ``` |
| 37 | +// r.ko.Spec.Logging = &svcapitypes.BucketLoggingStatus{} |
| 38 | +// r.ko.Spec.Logging.LoggingEnabled = &svcapitypes.LoggingEnabled{} |
| 39 | +// ``` |
| 40 | +func InitializeNestedStructField( |
| 41 | + r *model.CRD, |
| 42 | + sourceVarName string, |
| 43 | + field *model.Field, |
| 44 | + // apiPkgAlias contains the imported package alias where the type definition |
| 45 | + // for nested structs is present. |
| 46 | + // ex: svcapitypes "github.com/aws-controllers-k8s/s3-controller/apis/v1alpha1" |
| 47 | + apiPkgAlias string, |
| 48 | + // Number of levels of indentation to use |
| 49 | + indentLevel int, |
| 50 | +) string { |
| 51 | + out := "" |
| 52 | + indent := strings.Repeat("\t", indentLevel) |
| 53 | + fieldPath := field.Path |
| 54 | + if fieldPath != "" { |
| 55 | + fp := fieldpath.FromString(fieldPath) |
| 56 | + if fp.Size() > 1 { |
| 57 | + // replace the front field name with front field shape name inside |
| 58 | + // the field path to construct the fieldShapePath |
| 59 | + front := fp.Front() |
| 60 | + frontField := r.Fields[front] |
| 61 | + if frontField == nil { |
| 62 | + panic(fmt.Sprintf("unable to find the field with name %s"+ |
| 63 | + " for fieldpath %s", front, fieldPath)) |
| 64 | + } |
| 65 | + if frontField.ShapeRef == nil { |
| 66 | + panic(fmt.Sprintf("nil ShapeRef for field %s", front)) |
| 67 | + } |
| 68 | + fieldShapePath := strings.Replace(fieldPath, front, |
| 69 | + frontField.ShapeRef.ShapeName, 1) |
| 70 | + fsp := fieldpath.FromString(fieldShapePath) |
| 71 | + var index int |
| 72 | + // Build the prefix to access elements in field path. |
| 73 | + // Use the front of fieldpath to determine whether the field is |
| 74 | + // a spec field or status field. |
| 75 | + elemAccessPrefix := sourceVarName |
| 76 | + if _, found := r.SpecFields[front]; found { |
| 77 | + elemAccessPrefix = fmt.Sprintf("%s%s", elemAccessPrefix, |
| 78 | + r.Config().PrefixConfig.SpecField) |
| 79 | + } else { |
| 80 | + elemAccessPrefix = fmt.Sprintf("%s%s", elemAccessPrefix, |
| 81 | + r.Config().PrefixConfig.StatusField) |
| 82 | + } |
| 83 | + var importPath string |
| 84 | + if apiPkgAlias != "" { |
| 85 | + importPath = fmt.Sprintf("%s.", apiPkgAlias) |
| 86 | + } |
| 87 | + // traverse over the fieldShapePath and initialize every element |
| 88 | + // except the last. |
| 89 | + for index < fsp.Size()-1 { |
| 90 | + elemName := fp.At(index) |
| 91 | + elemShapeRef := fsp.ShapeRefAt(frontField.ShapeRef, index) |
| 92 | + if elemShapeRef.Shape.Type != "structure" { |
| 93 | + panic(fmt.Sprintf("only nested structures are supported."+ |
| 94 | + " Shape type for %s is %s inside fieldpath %s", elemName, |
| 95 | + elemShapeRef.Shape.Type, fieldPath)) |
| 96 | + } |
| 97 | + out += fmt.Sprintf("%s%s.%s = &%s%s{}\n", |
| 98 | + indent, elemAccessPrefix, elemName, importPath, |
| 99 | + elemShapeRef.GoTypeElem()) |
| 100 | + elemAccessPrefix = fmt.Sprintf("%s.%s", elemAccessPrefix, |
| 101 | + elemName) |
| 102 | + index++ |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return out |
| 107 | +} |
0 commit comments