Skip to content

Instantly share code, notes, and snippets.

@GrimTheReaper
Last active January 15, 2018 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrimTheReaper/baa5a3acfde9a2122d00392e2394d10f to your computer and use it in GitHub Desktop.
Save GrimTheReaper/baa5a3acfde9a2122d00392e2394d10f to your computer and use it in GitHub Desktop.
Partially Blind Unmarshaling with Golang.
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
}
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