|
8 | 8 |
|
9 | 9 | "github.com/kong/deck/file" |
10 | 10 | "github.com/kong/go-kong/kong" |
11 | | - "github.com/tidwall/gjson" |
12 | 11 | ) |
13 | 12 |
|
14 | 13 | // GenerateSHA generates a SHA256 checksum of the (targetContent, customEntities) tuple, with the purpose of change |
@@ -117,63 +116,3 @@ func PluginString(plugin file.FPlugin) string { |
117 | 116 | } |
118 | 117 | return result |
119 | 118 | } |
120 | | - |
121 | | -// FillPluginConfig returns a copy of `config` that has default values filled in from `schema`. |
122 | | -func FillPluginConfig(schema map[string]interface{}, |
123 | | - config kong.Configuration, |
124 | | -) (kong.Configuration, error) { |
125 | | - jsonb, err := json.Marshal(&schema) |
126 | | - if err != nil { |
127 | | - return nil, err |
128 | | - } |
129 | | - // Get all in the schema |
130 | | - value := gjson.ParseBytes((jsonb)) |
131 | | - return fillRecord(value, config) |
132 | | -} |
133 | | - |
134 | | -func fillRecord(schema gjson.Result, config kong.Configuration) (kong.Configuration, error) { |
135 | | - if config == nil { |
136 | | - return nil, nil |
137 | | - } |
138 | | - res := config.DeepCopy() |
139 | | - value := schema.Get("fields") |
140 | | - |
141 | | - value.ForEach(func(key, value gjson.Result) bool { |
142 | | - // get the key name |
143 | | - ms := value.Map() |
144 | | - fname := "" |
145 | | - for k := range ms { |
146 | | - fname = k |
147 | | - break |
148 | | - } |
149 | | - ftype := value.Get(fname + ".type") |
150 | | - if ftype.String() == "record" { |
151 | | - subConfig := config[fname] |
152 | | - if subConfig == nil { |
153 | | - subConfig = make(map[string]interface{}) |
154 | | - } |
155 | | - newSubConfig, err := fillRecord(value.Get(fname), subConfig.(map[string]interface{})) |
156 | | - if err != nil { |
157 | | - panic(err) |
158 | | - } |
159 | | - res[fname] = map[string]interface{}(newSubConfig) |
160 | | - return true |
161 | | - } |
162 | | - // check if key is already set in the config |
163 | | - if _, ok := config[fname]; ok { |
164 | | - // yes, don't set it |
165 | | - return true |
166 | | - } |
167 | | - // no, set it |
168 | | - value = value.Get(fname + ".default") |
169 | | - if value.Exists() { |
170 | | - res[fname] = value.Value() |
171 | | - } else { |
172 | | - // if no default exists, set an explicit nil |
173 | | - res[fname] = nil |
174 | | - } |
175 | | - return true |
176 | | - }) |
177 | | - |
178 | | - return res, nil |
179 | | -} |
0 commit comments