-
Notifications
You must be signed in to change notification settings - Fork 54
Unmarshal instance metadata JSON #40
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
base: master
Are you sure you want to change the base?
Changes from all commits
a5cca36
bb80962
cfebc47
a67a27c
558b027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,14 +105,19 @@ func parsePort(s string) int { | |
| // the raw JSON for later parsing. | ||
| func (i *InstanceMetadata) UnmarshalJSON(b []byte) error { | ||
| i.Raw = b | ||
| // TODO(cq) could actually parse Raw here, and in a parallel UnmarshalXML as well. | ||
| // TODO(cq) could actually parse Raw in a parallel UnmarshalXML as well. | ||
| var m map[string]interface{} | ||
| if err := json.Unmarshal(b, &m); err != nil { | ||
| return err | ||
| } | ||
| i.Parsed = m | ||
| return nil | ||
| } | ||
|
|
||
| // MarshalJSON is a custom JSON marshaler for InstanceMetadata. | ||
| func (i *InstanceMetadata) MarshalJSON() ([]byte, error) { | ||
| if i.parsed != nil { | ||
| return json.Marshal(i.parsed) | ||
| if i.Parsed != nil { | ||
| return json.Marshal(i.Parsed) | ||
| } | ||
|
|
||
| if i.Raw == nil { | ||
|
|
@@ -126,8 +131,8 @@ func (i *InstanceMetadata) MarshalJSON() ([]byte, error) { | |
| func (i InstanceMetadata) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | ||
| tokens := []xml.Token{start} | ||
|
|
||
| if i.parsed != nil { | ||
| for key, value := range i.parsed { | ||
| if i.Parsed != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same comment about checking the entry count applies here. But note that the |
||
| for key, value := range i.Parsed { | ||
| t := xml.StartElement{Name: xml.Name{"", key}} | ||
| tokens = append(tokens, t, xml.CharData(value.(string)), xml.EndElement{t.Name}) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
|
|
@@ -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{} { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that be checking the entry count instead? That is, if there's a map instance present, but it's empty, should that still be marshaled?