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

Unmarshal instance metadata JSON #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ func (a *Application) ParseAllMetadata() error {

// SetMetadataString for a given instance before register
func (ins *Instance) SetMetadataString(key, value string) {
if ins.Metadata.parsed == nil {
ins.Metadata.parsed = map[string]interface{}{}
if ins.Metadata.Parsed == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we can set entries in this field, "Parsed" doesn't match its use. "Parsed" implies that we only receive these values.

Again, consider renaming it to "Map", "Fields", "Values", or something similar.

ins.Metadata.Parsed = map[string]interface{}{}
}
ins.Metadata.parsed[key] = value
ins.Metadata.Parsed[key] = value
}

func (im *InstanceMetadata) parse() error {
if len(im.Raw) == 0 {
im.parsed = make(map[string]interface{})
im.Parsed = make(map[string]interface{})
log.Debug("len(Metadata)==0. Quitting parsing.")
return nil
}
metadataLog.Debugf("InstanceMetadata.parse: %s", im.Raw)

if len(im.Raw) > 0 && im.Raw[0] == '{' {
// JSON
err := json.Unmarshal(im.Raw, &im.parsed)
err := json.Unmarshal(im.Raw, &im.Parsed)
if err != nil {
log.Errorf("Error unmarshalling: %s", err.Error())
return fmt.Errorf("error unmarshalling: %s", err.Error())
Expand All @@ -52,22 +52,22 @@ func (im *InstanceMetadata) parse() error {
log.Errorf("Error unmarshalling: %s", err.Error())
return fmt.Errorf("error unmarshalling: %s", err.Error())
}
im.parsed = parsedDoc["d"].(map[string]interface{})
im.Parsed = parsedDoc["d"].(map[string]interface{})
}
return nil
}

// GetMap returns a map of the metadata parameters for this instance
func (im *InstanceMetadata) GetMap() map[string]interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the underlying field is exported, this method isn't necessary, but I doubt we can get away with removing it, as that would break existing callers.

return im.parsed
return im.Parsed
}

func (im *InstanceMetadata) getItem(key string) (interface{}, bool, error) {
err := im.parse()
if err != nil {
return "", false, fmt.Errorf("parsing error: %s", err.Error())
}
val, present := im.parsed[key]
val, present := im.Parsed[key]
return val, present, nil
}

Expand Down