Skip to content
Open
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
15 changes: 10 additions & 5 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

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?

if len(i.Parsed) > 0 {

return json.Marshal(i.Parsed)
}

if i.Raw == nil {
Expand All @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 range expression on line 135 would still work fine (that is, never executing the surrounding for expression's body) against a nil or empty map reference.

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})
}
Expand Down
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
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type Port struct {
// metadata.go for more info.
type InstanceMetadata struct {
Raw []byte `xml:",innerxml" json:"-"`
parsed map[string]interface{}
Parsed map[string]interface{} `json:"-"`
}

// AmazonMetadataType is information about AZ's, AMI's, and the AWS instance
Expand Down