|
| 1 | +/* |
| 2 | +Copyright 2018 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package yamltags |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +// ProcessStruct validates and processes the provided pointer to a struct. |
| 28 | +func ProcessStruct(s interface{}) error { |
| 29 | + parentStruct := reflect.ValueOf(s).Elem() |
| 30 | + t := parentStruct.Type() |
| 31 | + |
| 32 | + // Loop through the fields on the struct, looking for tags. |
| 33 | + for i := 0; i < t.NumField(); i++ { |
| 34 | + f := t.Field(i) |
| 35 | + val := parentStruct.Field(i) |
| 36 | + field := parentStruct.Type().Field(i) |
| 37 | + if tags, ok := f.Tag.Lookup("yamltags"); ok { |
| 38 | + if err := ProcessTags(tags, val, parentStruct, field); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + } |
| 42 | + // Recurse down the struct |
| 43 | + if val.Kind() == reflect.Struct { |
| 44 | + if err := ProcessStruct(val.Addr().Interface()); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func ProcessTags(yamltags string, val reflect.Value, parentStruct reflect.Value, field reflect.StructField) error { |
| 53 | + tags := strings.Split(yamltags, ",") |
| 54 | + for _, tag := range tags { |
| 55 | + tagParts := strings.Split(tag, "=") |
| 56 | + var yt YamlTag |
| 57 | + switch tagParts[0] { |
| 58 | + case "required": |
| 59 | + yt = &RequiredTag{} |
| 60 | + case "default": |
| 61 | + yt = &DefaultTag{} |
| 62 | + case "oneOf": |
| 63 | + yt = &OneOfTag{ |
| 64 | + Field: field, |
| 65 | + Parent: parentStruct, |
| 66 | + } |
| 67 | + } |
| 68 | + if err := yt.Load(tagParts); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if err := yt.Process(val); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +type YamlTag interface { |
| 79 | + Load([]string) error |
| 80 | + Process(reflect.Value) error |
| 81 | +} |
| 82 | + |
| 83 | +type RequiredTag struct { |
| 84 | +} |
| 85 | + |
| 86 | +func (rt *RequiredTag) Load(s []string) error { |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func (rt *RequiredTag) Process(val reflect.Value) error { |
| 91 | + if isZeroValue(val) { |
| 92 | + return errors.New("required value not set") |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +type DefaultTag struct { |
| 98 | + dv string |
| 99 | +} |
| 100 | + |
| 101 | +func (dt *DefaultTag) Load(s []string) error { |
| 102 | + if len(s) != 2 { |
| 103 | + return fmt.Errorf("invalid default tag: %v, expected key=value", s) |
| 104 | + } |
| 105 | + dt.dv = s[1] |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (dt *DefaultTag) Process(val reflect.Value) error { |
| 110 | + if !isZeroValue(val) { |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + switch val.Kind() { |
| 115 | + case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64: |
| 116 | + i, err := strconv.ParseInt(dt.dv, 0, 0) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + val.SetInt(i) |
| 121 | + case reflect.String: |
| 122 | + val.SetString(dt.dv) |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// A program can have many structs, that each have many oneOfSets |
| 128 | +// each oneOfSet is a map of a set name to the list of fields that belong to that set |
| 129 | +// only one field in that list can have a non-zero value. |
| 130 | + |
| 131 | +var allOneOfs map[string]map[string][]string |
| 132 | + |
| 133 | +func getOneOfSetsForStruct(structName string) map[string][]string { |
| 134 | + _, ok := allOneOfs[structName] |
| 135 | + if !ok { |
| 136 | + allOneOfs[structName] = map[string][]string{} |
| 137 | + } |
| 138 | + return allOneOfs[structName] |
| 139 | +} |
| 140 | + |
| 141 | +type OneOfTag struct { |
| 142 | + Field reflect.StructField |
| 143 | + Parent reflect.Value |
| 144 | + oneOfSets map[string][]string |
| 145 | + setName string |
| 146 | +} |
| 147 | + |
| 148 | +func (oot *OneOfTag) Load(s []string) error { |
| 149 | + if len(s) != 2 { |
| 150 | + return fmt.Errorf("invalid default struct tag: %v, expected key=value", s) |
| 151 | + } |
| 152 | + oot.setName = s[1] |
| 153 | + |
| 154 | + // Fetch the right oneOfSet for the struct. |
| 155 | + structName := oot.Parent.Type().Name() |
| 156 | + oot.oneOfSets = getOneOfSetsForStruct(structName) |
| 157 | + |
| 158 | + // Add this field to the oneOfSet |
| 159 | + oot.oneOfSets[oot.setName] = append(oot.oneOfSets[oot.setName], oot.Field.Name) |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func (oot *OneOfTag) Process(val reflect.Value) error { |
| 164 | + if isZeroValue(val) { |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + // This must exist because process is always called after Load. |
| 169 | + oneOfSet := oot.oneOfSets[oot.setName] |
| 170 | + for _, otherField := range oneOfSet { |
| 171 | + if otherField == oot.Field.Name { |
| 172 | + continue |
| 173 | + } |
| 174 | + field := oot.Parent.FieldByName(otherField) |
| 175 | + if !isZeroValue(field) { |
| 176 | + return fmt.Errorf("only one element in set %s can be set. got %s and %s", oot.setName, otherField, oot.Field.Name) |
| 177 | + } |
| 178 | + } |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func isZeroValue(val reflect.Value) bool { |
| 183 | + zv := reflect.Zero(val.Type()).Interface() |
| 184 | + return reflect.DeepEqual(zv, val.Interface()) |
| 185 | +} |
| 186 | + |
| 187 | +func init() { |
| 188 | + allOneOfs = make(map[string]map[string][]string) |
| 189 | +} |
0 commit comments