Last active
January 15, 2018 19:46
-
-
Save GrimTheReaper/baa5a3acfde9a2122d00392e2394d10f to your computer and use it in GitHub Desktop.
Partially Blind Unmarshaling with Golang.
This file contains hidden or 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
package triggertypes | |
import "time" | |
// RequestMade ... | |
type RequestMade struct { | |
Requester int `json:"requester"` // sapphire userID | |
RequestID int `json:"requestID"` | |
EventID int `json:"eventID"` | |
CreatedDate *time.Time `json:"createdDate"` // Here just incase its lost or delayed. | |
} | |
const requestMadeType = "RequestMade" | |
// GetRequestMadeTypeString ... | |
func GetRequestMadeTypeString() string { | |
return requestMadeType | |
} | |
// GetTypeString ... | |
func (trigger *RequestMade) GetTypeString() string { | |
return requestMadeType | |
} |
This file contains hidden or 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
package triggertypes | |
import ( | |
"encoding/json" | |
"errors" | |
"io" | |
) | |
// Unmarshal ... | |
func Unmarshal(triggerType string, reader io.ReadCloser) (t interface{}, err error) { | |
t = nil | |
decoder := json.NewDecoder(reader) | |
switch triggerType { | |
case requestMadeType: | |
t = &RequestMade{} | |
return | |
} | |
if t != nil { | |
err = decoder.Decode(&t) | |
if err != nil { | |
return nil, err | |
} | |
} | |
return nil, errors.New("Unknown type") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment