|
| 1 | +package schema |
| 2 | + |
| 3 | +type nestingMode uint8 |
| 4 | + |
| 5 | +const ( |
| 6 | + nestingModeSingle nestingMode = 0 |
| 7 | + nestingModeList nestingMode = 1 |
| 8 | + nestingModeSet nestingMode = 2 |
| 9 | + nestingModeMap nestingMode = 3 |
| 10 | +) |
| 11 | + |
| 12 | +// NestedAttributes surfaces a group of attributes to nest beneath another |
| 13 | +// attribute, and how that nesting should behave. Nesting can have the |
| 14 | +// following modes: |
| 15 | +// |
| 16 | +// * SingleNestedAttributes are nested attributes that represent a struct or |
| 17 | +// object; there should only be one instance of them nested beneath that |
| 18 | +// specific attribute. |
| 19 | +// |
| 20 | +// * ListNestedAttributes are nested attributes that represent a list of |
| 21 | +// structs or objects; there can be multiple instances of them beneath that |
| 22 | +// specific attribute. |
| 23 | +// |
| 24 | +// * SetNestedAttributes are nested attributes that represent a set of structs |
| 25 | +// or objects; there can be multiple instances of them beneath that specific |
| 26 | +// attribute. Unlike ListNestedAttributes, these nested attributes must have |
| 27 | +// unique values. |
| 28 | +// |
| 29 | +// * MapNestedAttributes are nested attributes that represent a string-indexed |
| 30 | +// map of structs or objects; there can be multiple instances of them beneath |
| 31 | +// that specific attribute. Unlike ListNestedAttributes, these nested |
| 32 | +// attributes must be associated with a unique key. Unlike SetNestedAttributes, |
| 33 | +// the key must be explicitly set by the user. |
| 34 | +type NestedAttributes interface { |
| 35 | + getNestingMode() nestingMode |
| 36 | + getAttributes() map[string]Attribute |
| 37 | +} |
| 38 | + |
| 39 | +type nestedAttributes map[string]Attribute |
| 40 | + |
| 41 | +func (n nestedAttributes) getAttributes() map[string]Attribute { |
| 42 | + return map[string]Attribute(n) |
| 43 | +} |
| 44 | + |
| 45 | +// SingleNestedAttributes nests `attributes` under another attribute, only |
| 46 | +// allowing one instance of that group of attributes to appear in the |
| 47 | +// configuration. |
| 48 | +func SingleNestedAttributes(attributes map[string]Attribute) NestedAttributes { |
| 49 | + return singleNestedAttributes{ |
| 50 | + nestedAttributes(attributes), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +type singleNestedAttributes struct { |
| 55 | + nestedAttributes |
| 56 | +} |
| 57 | + |
| 58 | +func (s singleNestedAttributes) getNestingMode() nestingMode { |
| 59 | + return nestingModeSingle |
| 60 | +} |
| 61 | + |
| 62 | +// ListNestedAttributes nests `attributes` under another attribute, allowing |
| 63 | +// multiple instances of that group of attributes to appear in the |
| 64 | +// configuration. Minimum and maximum numbers of times the group can appear in |
| 65 | +// the configuration can be set using `opts`. |
| 66 | +func ListNestedAttributes(attributes map[string]Attribute, opts ListNestedAttributesOptions) NestedAttributes { |
| 67 | + return listNestedAttributes{ |
| 68 | + nestedAttributes: nestedAttributes(attributes), |
| 69 | + min: opts.MinItems, |
| 70 | + max: opts.MaxItems, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +type listNestedAttributes struct { |
| 75 | + nestedAttributes |
| 76 | + |
| 77 | + min, max int |
| 78 | +} |
| 79 | + |
| 80 | +// ListNestedAttributesOptions captures additional, optional parameters for |
| 81 | +// ListNestedAttributes. |
| 82 | +type ListNestedAttributesOptions struct { |
| 83 | + MinItems int |
| 84 | + MaxItems int |
| 85 | +} |
| 86 | + |
| 87 | +func (l listNestedAttributes) getNestingMode() nestingMode { |
| 88 | + return nestingModeList |
| 89 | +} |
| 90 | + |
| 91 | +// SetNestedAttributes nests `attributes` under another attribute, allowing |
| 92 | +// multiple instances of that group of attributes to appear in the |
| 93 | +// configuration, while requiring each group of values be unique. Minimum and |
| 94 | +// maximum numbers of times the group can appear in the configuration can be |
| 95 | +// set using `opts`. |
| 96 | +func SetNestedAttributes(attributes map[string]Attribute, opts SetNestedAttributesOptions) NestedAttributes { |
| 97 | + return setNestedAttributes{ |
| 98 | + nestedAttributes: nestedAttributes(attributes), |
| 99 | + min: opts.MinItems, |
| 100 | + max: opts.MaxItems, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +type setNestedAttributes struct { |
| 105 | + nestedAttributes |
| 106 | + |
| 107 | + min, max int |
| 108 | +} |
| 109 | + |
| 110 | +// SetNestedAttributesOptions captures additional, optional parameters for |
| 111 | +// SetNestedAttributes. |
| 112 | +type SetNestedAttributesOptions struct { |
| 113 | + MinItems int |
| 114 | + MaxItems int |
| 115 | +} |
| 116 | + |
| 117 | +func (s setNestedAttributes) getNestingMode() nestingMode { |
| 118 | + return nestingModeSet |
| 119 | +} |
| 120 | + |
| 121 | +// MapNestedAttributes nests `attributes` under another attribute, allowing |
| 122 | +// multiple instances of that group of attributes to appear in the |
| 123 | +// configuration. Each group will need to be associated with a unique string by |
| 124 | +// the user. Minimum and maximum numbers of times the group can appear in the |
| 125 | +// configuration can be set using `opts`. |
| 126 | +func MapNestedAttributes(attributes map[string]Attribute, opts MapNestedAttributesOptions) NestedAttributes { |
| 127 | + return mapNestedAttributes{ |
| 128 | + nestedAttributes: nestedAttributes(attributes), |
| 129 | + min: opts.MinItems, |
| 130 | + max: opts.MaxItems, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +type mapNestedAttributes struct { |
| 135 | + nestedAttributes |
| 136 | + |
| 137 | + min, max int |
| 138 | +} |
| 139 | + |
| 140 | +// MapNestedAttributesOptions captures additional, optional parameters for |
| 141 | +// MapNestedAttributes. |
| 142 | +type MapNestedAttributesOptions struct { |
| 143 | + MinItems int |
| 144 | + MaxItems int |
| 145 | +} |
| 146 | + |
| 147 | +func (m mapNestedAttributes) getNestingMode() nestingMode { |
| 148 | + return nestingModeMap |
| 149 | +} |
0 commit comments