-
Notifications
You must be signed in to change notification settings - Fork 2
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
100 msg to json #105
100 msg to json #105
Conversation
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.
Couple minor suggestions
msg/message.go
Outdated
// ProtocolError is an error that occured during a request. | ||
type ProtocolError struct { | ||
Code ErrorCode | ||
Message string | ||
Code ErrorCode `json:"Code"` |
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.
If the json struct tag is the same as the struct field name, then it can be omitted. I'd say either remove the struct tags or make them camelCase
like typical JSON
msg/message.go
Outdated
ID string | ||
ResourceType ResourceType | ||
Params map[string]interface{} | ||
ID string `json:"ID"` |
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.
same point applies in these structs for json tags
peer/peer.go
Outdated
@@ -223,17 +223,18 @@ func (p *Peer) Dispatch() { | |||
} | |||
errCount = 0 | |||
|
|||
switch message.(type) { | |||
switch payload.(type) { |
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.
Nice, I like how you avoided needing changes outside message
package
@@ -316,15 +315,19 @@ func MaintainConnections() { | |||
// PeerInfoHandler will handle the response to a PeerInfo request by attempting | |||
// to establish connections with all new peers in the given response Resource. | |||
func PeerInfoHandler(res *msg.Response) { | |||
peers := res.Resource.([]string) | |||
peers := res.Resource.([]interface{}) |
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.
Why is this necessary?
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 because when arrays are unmarshalled from JSON they become []interface{}
in Go.
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.
Right but we're type casting them. Why can't we type cast it directly to []string
like before rather than type casting each element individually?
peer/peer.go
Outdated
|
||
for !receivedAddr || !sentAddr { | ||
message, err := msg.Read(c) | ||
payload, err := msg.Read(c) |
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.
Minor thing but I think calling this message
still makes sense. Technically we're extracting the "payload" from the "message" in the message package but from the context of peer this is a message.
Related Issue
#100
Description
Message
type (rather than the interface we used before) that holds the payload type and the payload in byte slice form. This allows us to correctly decode the payload when a message is received.Push
,Request
, andResponse
.Read
function will return aMessagePayload
(essentially just an interface that wraps the Request, Response, and Push concrete types).Read
can switch on the returned payload type to retrieve its underlying concrete type.WIKI Updates
Todos
None. Cuz i get werk done.