|
| 1 | +package cmsjson |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | +) |
| 7 | + |
| 8 | +type ( |
| 9 | + // jsonNode is the internal implementation of AstNode, *jsonNode @implements AstNode |
| 10 | + // AstNode is a simple interface that represents a node in our JSON AST, we have a few important constraints that should be enforced by any implementation of the AstNode, those constraints are: |
| 11 | + // - An ASTNode is either a: JsonPrimitive, JsonObject or a JsonArray |
| 12 | + // - GetKey can return nil indicating that it is JUST a value |
| 13 | + // - Since a node can be either a JsonPrimitive, JsonObject or a JsonArray: |
| 14 | + // - 2 of the three functions: JsonPrimitive(), JsonObject(), JsonArray() will return nil (indicating the node is not of that type) while one will return an actual value |
| 15 | + // - We are guaranteed that one of these functions will return a value |
| 16 | + // - All implementations of AstNode must conform to this specification (there is no way within the Go type system to enforce this unfortunately :( ) |
| 17 | + // - Note that the reflect.Type returned by JsonArray is the type of the array, ie if it was an array of integers then the reflect.type is an integer |
| 18 | + // - Note that jsonNode implements AstNode (indirectly), AstNode is of the form: |
| 19 | + // AstNode interface { |
| 20 | + // GetKey() string |
| 21 | + // |
| 22 | + // JsonPrimitive() (interface{}, reflect.Type) |
| 23 | + // JsonObject() ([]AstNode, reflect.Type) |
| 24 | + // JsonArray() ([]AstNode, reflect.Type) |
| 25 | + // } |
| 26 | + jsonNode struct { |
| 27 | + // key could be nil (according to the AstNode definition) |
| 28 | + key string |
| 29 | + |
| 30 | + // either value or children can be nil (according to the AstNode definition) |
| 31 | + value interface{} |
| 32 | + children []*jsonNode |
| 33 | + |
| 34 | + // underlying type is the type modelled by this jsonNode, isObject allows us distinguish between arrays and objects |
| 35 | + underlyingType reflect.Type |
| 36 | + isObject bool |
| 37 | + } |
| 38 | + |
| 39 | + // jsonPrimitives is a generic constraint for json primitive values |
| 40 | + jsonPrimitives interface { |
| 41 | + ~int | ~float64 | ~bool | ~string |
| 42 | + } |
| 43 | +) |
| 44 | + |
| 45 | +// Interface implementations for AstNode |
| 46 | + |
| 47 | +// GetKey returns the key of the underlying jsonNode |
| 48 | +func (node *jsonNode) GetKey() string { return node.key } |
| 49 | + |
| 50 | +// JsonPrimitive returns the underlying primitive value in a jsonNode, it either returns the value or nil in accordance with the |
| 51 | +// definition of the AstNode |
| 52 | +func (node *jsonNode) GetPrimitive() (interface{}, reflect.Type) { |
| 53 | + node.validateNode() |
| 54 | + if node.value != nil { |
| 55 | + return node.value, node.underlyingType |
| 56 | + } |
| 57 | + |
| 58 | + return nil, nil |
| 59 | +} |
| 60 | + |
| 61 | +// JsonObject returns the underlying json object in a jsonNode, it either returns the value or nil in accordance with the |
| 62 | +// definition of the AstNode |
| 63 | +func (node *jsonNode) JsonObject() ([]*jsonNode, reflect.Type) { |
| 64 | + node.validateNode() |
| 65 | + if node.children != nil && node.isObject { |
| 66 | + return node.children, node.underlyingType |
| 67 | + } |
| 68 | + |
| 69 | + return nil, nil |
| 70 | +} |
| 71 | + |
| 72 | +// JsonArray returns the underlying json array in a jsonNode, it either returns the value or nil in accordance with the |
| 73 | +// definition of the AstNode |
| 74 | +func (node *jsonNode) JsonArray() ([]*jsonNode, reflect.Type) { |
| 75 | + node.validateNode() |
| 76 | + if node.children != nil && !node.isObject { |
| 77 | + return node.children, node.underlyingType |
| 78 | + } |
| 79 | + |
| 80 | + return nil, nil |
| 81 | +} |
| 82 | + |
| 83 | +// validateNode determines if the current node configuration was corrupted or not |
| 84 | +func (node *jsonNode) validateNode() { |
| 85 | + if (node.value == nil && node.children == nil) || (node.value != nil && node.children != nil) { |
| 86 | + panic(fmt.Errorf("the provided error configuration: %v was corrupted somehow", *node)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// General functions for creating instances of jsonNode |
| 91 | + |
| 92 | +// newJsonArray constructs a new instance of a JsonArray given the array of json values it contains |
| 93 | +// note that there is no validation to ensure that the fields match the incoming |
| 94 | +// underlyingType |
| 95 | +func newJsonArray(key string, values []*jsonNode, underlyingType reflect.Type) *jsonNode { |
| 96 | + return &jsonNode{ |
| 97 | + key: key, |
| 98 | + value: nil, |
| 99 | + |
| 100 | + children: values, |
| 101 | + underlyingType: underlyingType, |
| 102 | + isObject: false, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// newJsonObject instantiates a new instance of a JsonObject type, note that there is no validation to ensure that the fields match the incoming |
| 107 | +// underlyingType |
| 108 | +func newJsonObject(key string, values []*jsonNode, underlyingType reflect.Type) *jsonNode { |
| 109 | + return &jsonNode{ |
| 110 | + key: key, |
| 111 | + value: nil, |
| 112 | + |
| 113 | + children: values, |
| 114 | + underlyingType: underlyingType, |
| 115 | + isObject: true, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// newJsonPrimitive instantiates a new instance of a jsonPrimitive type, note that this method has no validation logic (perhaps we can add it in the future) |
| 120 | +func newJsonPrimitive(key string, value interface{}, underlyingType reflect.Type) *jsonNode { |
| 121 | + return &jsonNode{ |
| 122 | + key: key, |
| 123 | + value: value, |
| 124 | + |
| 125 | + children: nil, |
| 126 | + underlyingType: underlyingType, |
| 127 | + isObject: false, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// InsertOrUpdate inserts a secondary json node into a jsonNode given the index in which it needs to be inserted, note that it also does type validation :D |
| 132 | +func (node *jsonNode) InsertOrUpdate(toInsert *jsonNode, location int) error { |
| 133 | + node.validateNode() |
| 134 | + if node.children != nil { |
| 135 | + return fmt.Errorf("node is a terminal primitive value %v, primitive values cannot have children", *node) |
| 136 | + } |
| 137 | + |
| 138 | + // validInsertions are characterized by inserting into a struct at the correct type of |
| 139 | + validInsert := (getStructFieldType(node.underlyingType, location) == toInsert.underlyingType && location < len(node.children)) || |
| 140 | + (node.underlyingType == toInsert.underlyingType && location <= len(node.children)) |
| 141 | + |
| 142 | + if validInsert { |
| 143 | + switch location { |
| 144 | + case len(node.children): |
| 145 | + node.children = append(node.children, toInsert) |
| 146 | + default: |
| 147 | + node.children = append(append(node.children[:location], toInsert), node.children[location:]...) |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | + } |
| 152 | + |
| 153 | + return fmt.Errorf("the insertion for %v index %d was invalid", *node, location) |
| 154 | +} |
| 155 | + |
| 156 | +// NewPrimitiveFromValue constructs a new jsonNode from a primitive value |
| 157 | +func NewPrimitiveFromValue[T jsonPrimitives](key string, value T) *jsonNode { |
| 158 | + return &jsonNode{ |
| 159 | + key: key, |
| 160 | + value: value, |
| 161 | + |
| 162 | + children: nil, |
| 163 | + underlyingType: reflect.TypeOf(value), |
| 164 | + isObject: false, |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// getStructFieldType fetches the field type of a struct given its index |
| 169 | +func getStructFieldType(structType reflect.Type, index int) reflect.Type { |
| 170 | + if structType.Kind() != reflect.Struct { |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + return structType.FieldByIndex([]int{index}).Type |
| 175 | +} |
0 commit comments