Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: un-deprecate api.DecodeConfigEntry #6278

Merged
merged 2 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions api/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,17 @@ func MakeConfigEntry(kind, name string) (ConfigEntry, error) {
return makeConfigEntry(kind, name)
}

// DEPRECATED: TODO(rb): remove?
// DecodeConfigEntry will decode the result of using json.Unmarshal of a config
// entry into a map[string]interface{}.
//
// DecodeConfigEntry only successfully works on config entry kinds
// "service-defaults" and "proxy-defaults" (as of Consul 1.5).
// Important caveats:
//
// This is because by parsing HCL into map[string]interface{} and then trying
// to decode it with mapstructure we run into the problem where hcl generically
// decodes many things into map[string][]interface{} at intermediate nodes in
// the resulting structure (for nested structs not otherwise in an enclosing
// slice). This breaks decoding.
// - This will NOT work if the map[string]interface{} was produced using HCL
// decoding as that requires more extensive parsing to work around the issues
// with map[string][]interface{} that arise.
//
// Until a better solution is arrived at don't use this method.
// - This will only decode fields using their camel case json field
// representations.
func DecodeConfigEntry(raw map[string]interface{}) (ConfigEntry, error) {
var entry ConfigEntry

Expand Down Expand Up @@ -188,6 +187,18 @@ func DecodeConfigEntryFromJSON(data []byte) (ConfigEntry, error) {
return DecodeConfigEntry(raw)
}

func decodeConfigEntrySlice(raw []map[string]interface{}) ([]ConfigEntry, error) {
var entries []ConfigEntry
for _, rawEntry := range raw {
entry, err := DecodeConfigEntry(rawEntry)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
return entries, nil
}

// ConfigEntries can be used to query the Config endpoints
type ConfigEntries struct {
c *Client
Expand Down Expand Up @@ -251,13 +262,9 @@ func (conf *ConfigEntries) List(kind string, q *QueryOptions) ([]ConfigEntry, *Q
return nil, nil, err
}

var entries []ConfigEntry
for _, rawEntry := range raw {
entry, err := DecodeConfigEntry(rawEntry)
if err != nil {
return nil, nil, err
}
entries = append(entries, entry)
entries, err := decodeConfigEntrySlice(raw)
if err != nil {
return nil, nil, err
}

return entries, qm, nil
Expand Down
Loading