|
| 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/model" |
| 21 | +) |
| 22 | + |
| 23 | +// GoCodeToACKTags returns Go code that converts Resource Tags |
| 24 | +// to ACK Tags. If Resource Tags field is of type list, we |
| 25 | +// also maintain and return the order of the list as a []string |
| 26 | +// |
| 27 | +// |
| 28 | +// |
| 29 | +// Sample output: |
| 30 | +// |
| 31 | +// for _, k := range keyOrder { |
| 32 | +// v, ok := tags[k] |
| 33 | +// if ok { |
| 34 | +// tag := svcapitypes.Tag{Key: &k, Value: &v} |
| 35 | +// result = append(result, &tag) |
| 36 | +// delete(tags, k) |
| 37 | +// } |
| 38 | +// } |
| 39 | +// for k, v := range tags { |
| 40 | +// tag := svcapitypes.Tag{Key: &k, Value: &v} |
| 41 | +// result = append(result, &tag) |
| 42 | +// } |
| 43 | +func GoCodeConvertToACKTags(r *model.CRD, sourceVarName string, targetVarName string, keyOrderVarName string, indentLevel int) string { |
| 44 | + |
| 45 | + out := "\n" |
| 46 | + indent := strings.Repeat("\t", indentLevel) |
| 47 | + tagField, err := r.GetTagField() |
| 48 | + if err != nil { |
| 49 | + panic("error: resource does not have tags. ignore in generator.yaml") |
| 50 | + } |
| 51 | + |
| 52 | + if tagField == nil { |
| 53 | + return "" |
| 54 | + } |
| 55 | + |
| 56 | + tagFieldShapeType := tagField.ShapeRef.Shape.Type |
| 57 | + keyMemberName := r.GetTagKeyMemberName() |
| 58 | + valueMemberName := r.GetTagValueMemberName() |
| 59 | + |
| 60 | + out += fmt.Sprintf("%sif len(%s) == 0 {\n", indent, sourceVarName) |
| 61 | + out += fmt.Sprintf("%s\treturn %s, %s\n", indent, targetVarName, keyOrderVarName) |
| 62 | + out += fmt.Sprintf("%s}\n", indent) |
| 63 | + |
| 64 | + switch tagFieldShapeType { |
| 65 | + case "list": |
| 66 | + out += fmt.Sprintf("%sfor _, t := range %s {\n", indent, sourceVarName) |
| 67 | + out += fmt.Sprintf("%s\tif t.%s != nil {\n", indent, keyMemberName) |
| 68 | + out += fmt.Sprintf("%s\t\t%s = append(%s, *t.%s)\n", indent, keyOrderVarName, keyOrderVarName, keyMemberName) |
| 69 | + out += fmt.Sprintf("%s\t\tif t.%s != nil {\n", indent, valueMemberName) |
| 70 | + out += fmt.Sprintf("%s\t\t\t%s[*t.%s] = *t.%s\n", indent, targetVarName, keyMemberName, valueMemberName) |
| 71 | + out += fmt.Sprintf("%s\t\t} else {\n", indent) |
| 72 | + out += fmt.Sprintf("%s\t\t\t%s[*t.%s] = \"\"\n", indent, targetVarName, keyMemberName) |
| 73 | + out += fmt.Sprintf("%s\t\t}\n", indent) |
| 74 | + out += fmt.Sprintf("%s\t}\n", indent) |
| 75 | + out += fmt.Sprintf("%s}\n", indent) |
| 76 | + |
| 77 | + case "map": |
| 78 | + out += fmt.Sprintf("%sfor k, v := range %s {\n", indent, sourceVarName) |
| 79 | + out += fmt.Sprintf("%s\tif v == nil {\n", indent) |
| 80 | + out += fmt.Sprintf("%s\t\t%s[k] = \"\"\n", indent, targetVarName) |
| 81 | + out += fmt.Sprintf("%s\t} else {\n", indent) |
| 82 | + out += fmt.Sprintf("%s\t\t%s[k] = *v\n", indent, targetVarName) |
| 83 | + out += fmt.Sprintf("%s\t}\n", indent) |
| 84 | + out += fmt.Sprintf("%s}\n", indent) |
| 85 | + default: |
| 86 | + msg := "error: tag type can only be a list or a map" |
| 87 | + panic(msg) |
| 88 | + } |
| 89 | + |
| 90 | + return out |
| 91 | +} |
| 92 | + |
| 93 | +// GoCodeFromACKTags returns Go code that converts ACKTags |
| 94 | +// to the Resource Tag shape type. Tag fields can only be |
| 95 | +// maps or lists of Tag Go type. If Tag field is a list, |
| 96 | +// when converting from ACK Tags, we try to preserve the |
| 97 | +// original order |
| 98 | +// |
| 99 | +// |
| 100 | +// |
| 101 | +// Sample output: |
| 102 | +// |
| 103 | +// for _, k := range keyOrder { |
| 104 | +// v, ok := tags[k] |
| 105 | +// if ok { |
| 106 | +// tag := svcapitypes.Tag{Key: &k, Value: &v} |
| 107 | +// result = append(result, &tag) |
| 108 | +// delete(tags, k) |
| 109 | +// } |
| 110 | +// } |
| 111 | +// for k, v := range tags { |
| 112 | +// tag := svcapitypes.Tag{Key: &k, Value: &v} |
| 113 | +// result = append(result, &tag) |
| 114 | +// } |
| 115 | +func GoCodeFromACKTags(r *model.CRD, tagsSourceVarName string, orderVarName string, targetVarName string, indentLevel int) string { |
| 116 | + out := "\n" |
| 117 | + indent := strings.Repeat("\t", indentLevel) |
| 118 | + tagField, _ := r.GetTagField() |
| 119 | + |
| 120 | + if tagField == nil { |
| 121 | + return "" |
| 122 | + } |
| 123 | + |
| 124 | + tagFieldShapeType := tagField.ShapeRef.Shape.Type |
| 125 | + tagFieldGoType := tagField.GoTypeElem |
| 126 | + keyMemberName := r.GetTagKeyMemberName() |
| 127 | + valueMemberName := r.GetTagValueMemberName() |
| 128 | + |
| 129 | + switch tagFieldShapeType { |
| 130 | + case "list": |
| 131 | + out += fmt.Sprintf("%sfor _, k := range %s {\n", indent, orderVarName) |
| 132 | + out += fmt.Sprintf("%s\tv, ok := %s[k]\n", indent, tagsSourceVarName) |
| 133 | + out += fmt.Sprintf("%s\tif ok {\n", indent) |
| 134 | + out += fmt.Sprintf("%s\t\ttag := svcapitypes.%s{%s: &k, %s: &v}\n", indent, tagFieldGoType, keyMemberName, valueMemberName) |
| 135 | + out += fmt.Sprintf("%s\t\t%s = append(%s, &tag)\n", indent, targetVarName, targetVarName) |
| 136 | + out += fmt.Sprintf("%s\t\tdelete(%s, k)\n", indent, tagsSourceVarName) |
| 137 | + out += fmt.Sprintf("%s\t}\n", indent) |
| 138 | + out += fmt.Sprintf("%s}\n", indent) |
| 139 | + case "map": |
| 140 | + out += fmt.Sprintf("%s_ = %s\n", indent, orderVarName) |
| 141 | + default: |
| 142 | + msg := "error: tag type can only be a list of a map" |
| 143 | + panic(msg) |
| 144 | + } |
| 145 | + |
| 146 | + out += fmt.Sprintf("%sfor k, v := range %s {\n", indent, tagsSourceVarName) |
| 147 | + switch tagFieldShapeType { |
| 148 | + case "list": |
| 149 | + out += fmt.Sprintf("%s\ttag := svcapitypes.%s{%s: &k, %s: &v}\n", indent, tagFieldGoType, keyMemberName, valueMemberName) |
| 150 | + out += fmt.Sprintf("%s\t%s = append(%s, &tag)\n", indent, targetVarName, targetVarName) |
| 151 | + case "map": |
| 152 | + out += fmt.Sprintf("%s\t%s[k] = &v\n", indent, targetVarName) |
| 153 | + } |
| 154 | + out += fmt.Sprintf("%s}\n", indent) |
| 155 | + |
| 156 | + return out |
| 157 | +} |
0 commit comments