(key string, zeroValue interface{}, resultType string, convert convertPrimitiveFunc, errorHolder *error)
| 51 | type convertPrimitiveFunc func(attributes Attributes, key string, attributeType string) (interface{}, error) |
| 52 | |
| 53 | func (attributes Attributes) getPrimitive(key string, zeroValue interface{}, resultType string, convert convertPrimitiveFunc, errorHolder *error) interface{} { |
| 54 | var err error |
| 55 | if attribute, exists := attributes[key]; exists { |
| 56 | var result interface{} |
| 57 | switch resultType { |
| 58 | case "string": |
| 59 | primitiveResult := new(string) |
| 60 | err = json.Unmarshal(attribute.Raw, primitiveResult) |
| 61 | result = *primitiveResult |
| 62 | case "boolean": |
| 63 | primitiveResult := new(bool) |
| 64 | err = json.Unmarshal(attribute.Raw, primitiveResult) |
| 65 | result = *primitiveResult |
| 66 | case "number": |
| 67 | primitiveResult := new(float64) |
| 68 | err = json.Unmarshal(attribute.Raw, primitiveResult) |
| 69 | result = *primitiveResult |
| 70 | } |
| 71 | if err == nil { |
| 72 | return result |
| 73 | } |
| 74 | |
| 75 | switch typeError := err.(type) { |
| 76 | case *json.UnmarshalTypeError: |
| 77 | convertedValue, retryError := convert(attributes, key, typeError.Value) |
| 78 | if retryError == nil && convertedValue != nil { |
| 79 | return convertedValue |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | err = &KeyNotFoundError{Key: key} |
| 84 | } |
| 85 | if errorHolder != nil { |
| 86 | *errorHolder = err |
| 87 | } |
| 88 | return zeroValue |
| 89 | } |
| 90 | |
| 91 | // GetString allows returning the attribute with the given key |
| 92 | // as a string. If the attribute JSON/YAML content is |
no outgoing calls
no test coverage detected