Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2019 The Kubernetes 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 value |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "reflect" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | // TODO: This implements the same functionality as https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go#L236 |
| 26 | // but is based on the highly efficient approach from https://golang.org/src/encoding/json/encode.go |
| 27 | |
| 28 | func lookupJsonTags(f reflect.StructField) (name string, omit bool, inline bool, omitempty bool) { |
| 29 | tag := f.Tag.Get("json") |
| 30 | if tag == "-" { |
| 31 | return "", true, false, false |
| 32 | } |
| 33 | name, opts := parseTag(tag) |
| 34 | if name == "" { |
| 35 | name = f.Name |
| 36 | } |
| 37 | return name, false, opts.Contains("inline"), opts.Contains("omitempty") |
| 38 | } |
| 39 | |
| 40 | func isZero(v reflect.Value) bool { |
| 41 | switch v.Kind() { |
| 42 | case reflect.Array, reflect.Map, reflect.Slice, reflect.String: |
| 43 | return v.Len() == 0 |
| 44 | case reflect.Bool: |
| 45 | return !v.Bool() |
| 46 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 47 | return v.Int() == 0 |
| 48 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 49 | return v.Uint() == 0 |
| 50 | case reflect.Float32, reflect.Float64: |
| 51 | return v.Float() == 0 |
| 52 | case reflect.Interface, reflect.Ptr: |
| 53 | return v.IsNil() |
| 54 | case reflect.Chan, reflect.Func: |
| 55 | panic(fmt.Sprintf("unsupported type: %v", v.Type())) |
| 56 | } |
| 57 | return false |
| 58 | } |
| 59 | |
| 60 | type tagOptions string |
| 61 | |
| 62 | // parseTag splits a struct field's json tag into its name and |
| 63 | // comma-separated options. |
| 64 | func parseTag(tag string) (string, tagOptions) { |
| 65 | if idx := strings.Index(tag, ","); idx != -1 { |
| 66 | return tag[:idx], tagOptions(tag[idx+1:]) |
| 67 | } |
| 68 | return tag, tagOptions("") |
| 69 | } |
| 70 | |
| 71 | // Contains reports whether a comma-separated list of options |
| 72 | // contains a particular substr flag. substr must be surrounded by a |
| 73 | // string boundary or commas. |
| 74 | func (o tagOptions) Contains(optionName string) bool { |
| 75 | if len(o) == 0 { |
| 76 | return false |
| 77 | } |
| 78 | s := string(o) |
| 79 | for s != "" { |
| 80 | var next string |
| 81 | i := strings.Index(s, ",") |
| 82 | if i >= 0 { |
| 83 | s, next = s[:i], s[i+1:] |
| 84 | } |
| 85 | if s == optionName { |
| 86 | return true |
| 87 | } |
| 88 | s = next |
| 89 | } |
| 90 | return false |
| 91 | } |