-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add JSON support: #6
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package fargo | ||
|
||
// MIT Licensed (see README.md) - Copyright (c) 2013 Hudl <@Hudl> | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
) | ||
|
||
// Temporary structs used for GetAppsResponse unmarshalling | ||
type getAppsResponseArray GetAppsResponse | ||
type getAppsResponseSingle struct { | ||
Application *Application `json:"application"` | ||
AppsHashcode string `json:"apps__hashcode"` | ||
VersionsDelta int `json:"versions__delta"` | ||
} | ||
|
||
// UnmarshalJSON is a custom JSON unmarshaler for GetAppsResponse to deal with | ||
// sometimes non-wrapped Application arrays when there is only a single Application item. | ||
func (r *GetAppsResponse) UnmarshalJSON(b []byte) error { | ||
marshalLog.Debug("GetAppsResponse.UnmarshalJSON b:%s\n", string(b)) | ||
var err error | ||
|
||
// Normal array case | ||
var ra getAppsResponseArray | ||
if err = json.Unmarshal(b, &ra); err == nil { | ||
marshalLog.Debug("GetAppsResponse.UnmarshalJSON ra:%+v\n", ra) | ||
*r = GetAppsResponse(ra) | ||
return nil | ||
} | ||
// Bogus non-wrapped case | ||
var rs getAppsResponseSingle | ||
if err = json.Unmarshal(b, &rs); err == nil { | ||
marshalLog.Debug("GetAppsResponse.UnmarshalJSON rs:%+v\n", rs) | ||
r.Applications = make([]*Application, 1, 1) | ||
r.Applications[0] = rs.Application | ||
r.AppsHashcode = rs.AppsHashcode | ||
r.VersionsDelta = rs.VersionsDelta | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// Temporary structs used for Application unmarshalling | ||
type applicationArray Application | ||
type applicationSingle struct { | ||
Name string `json:"name"` | ||
Instance *Instance `json:"instance"` | ||
} | ||
|
||
// UnmarshalJSON is a custom JSON unmarshaler for Application to deal with | ||
// sometimes non-wrapped Instance array when there is only a single Instance item. | ||
func (a *Application) UnmarshalJSON(b []byte) error { | ||
marshalLog.Debug("Application.UnmarshalJSON b:%s\n", string(b)) | ||
var err error | ||
|
||
// Normal array case | ||
var aa applicationArray | ||
if err = json.Unmarshal(b, &aa); err == nil { | ||
marshalLog.Debug("Application.UnmarshalJSON aa:%+v\n", aa) | ||
*a = Application(aa) | ||
return nil | ||
} | ||
|
||
// Bogus non-wrapped case | ||
var as applicationSingle | ||
if err = json.Unmarshal(b, &as); err == nil { | ||
marshalLog.Debug("Application.UnmarshalJSON as:%+v\n", as) | ||
a.Name = as.Name | ||
a.Instances = make([]*Instance, 1, 1) | ||
a.Instances[0] = as.Instance | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
type instance Instance | ||
|
||
// UnmarshalJSON is a custom JSON unmarshaler for Instance to deal with the | ||
// different Port encodings between XML and JSON. Here we copy the values from the JSON | ||
// Port struct into the simple XML int field. | ||
func (i *Instance) UnmarshalJSON(b []byte) error { | ||
var err error | ||
var ii instance | ||
if err = json.Unmarshal(b, &ii); err == nil { | ||
marshalLog.Debug("Instance.UnmarshalJSON ii:%+v\n", ii) | ||
*i = Instance(ii) | ||
i.Port = parsePort(ii.PortJ.Number) | ||
i.SecurePort = parsePort(ii.SecurePortJ.Number) | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
func parsePort(s string) int { | ||
n, err := strconv.Atoi(s) | ||
if err != nil { | ||
log.Warning("Invalid port error: %s", err.Error()) | ||
} | ||
return n | ||
} | ||
|
||
// UnmarshalJSON is a custom JSON unmarshaler for InstanceMetadata to handle squirreling away | ||
// 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. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package fargo | |
// MIT Licensed (see README.md) - Copyright (c) 2013 Hudl <@Hudl> | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/clbanning/x2j" | ||
) | ||
|
@@ -12,27 +13,39 @@ func (a *Application) ParseAllMetadata() error { | |
for _, instance := range a.Instances { | ||
err := instance.Metadata.parse() | ||
if err != nil { | ||
log.Error("Failed parsing metadata for Instance=%s of Application=%s: ", instance.HostName, a.Name, err.Error()) | ||
log.Error("Failed parsing metadata for Instance=%s of Application=%s: %s", | ||
instance.HostName, a.Name, err.Error()) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (im *InstanceMetadata) parse() error { | ||
// wrap in a BS xml tag so all metadata tags are pulled | ||
if len(im.Raw) == 0 { | ||
im.parsed = make(map[string]interface{}) | ||
log.Debug("len(Metadata)==0. Quitting parsing.") | ||
return nil | ||
} | ||
fullDoc := append(append([]byte("<d>"), im.Raw...), []byte("</d>")...) | ||
parsedDoc, err := x2j.ByteDocToMap(fullDoc, true) | ||
if err != nil { | ||
log.Error("Error unmarshalling: ", err.Error()) | ||
return fmt.Errorf("error unmarshalling: ", err.Error()) | ||
metadataLog.Debug("InstanceMetadata.parse: %s", im.Raw) | ||
|
||
if len(im.Raw) > 0 && im.Raw[0] == '{' { | ||
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. Simplistic way of deciding if the raw payload is JSON or XML. We could actually do each of them in their own custom unmarshallers. |
||
// JSON | ||
err := json.Unmarshal(im.Raw, &im.parsed) | ||
if err != nil { | ||
log.Error("Error unmarshalling: %s", err.Error()) | ||
return fmt.Errorf("error unmarshalling: %s", err.Error()) | ||
} | ||
} else { | ||
// XML: wrap in a BS xml tag so all metadata tags are pulled | ||
fullDoc := append(append([]byte("<d>"), im.Raw...), []byte("</d>")...) | ||
parsedDoc, err := x2j.ByteDocToMap(fullDoc, true) | ||
if err != nil { | ||
log.Error("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 | ||
} | ||
|
||
|
@@ -44,7 +57,7 @@ func (im *InstanceMetadata) GetMap() map[string]interface{} { | |
func (im *InstanceMetadata) getItem(key string) (interface{}, bool, error) { | ||
err := im.parse() | ||
if err != nil { | ||
return "", false, fmt.Errorf("parsing error: ", err.Error()) | ||
return "", false, fmt.Errorf("parsing error: %s", err.Error()) | ||
} | ||
val, present := im.parsed[key] | ||
return val, present, nil | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is where I put the custom JSON unmarshalling