Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created August 22, 2018 03:20
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 eklitzke/deaefd23a5a1b31a5c320abc12c1595c to your computer and use it in GitHub Desktop.
Save eklitzke/deaefd23a5a1b31a5c320abc12c1595c to your computer and use it in GitHub Desktop.
// PushType represents a value that can be pushed into the hierarchy.
type PushType map[string]interface{}
func init() {
gob.Register(PushType{})
}
var errBadPushCast = errors.New("bad push cast")
// go is so fucking stupid
func castToPushType(val interface{}) (PushType, error) {
var p PushType
if mapVal, ok := val.(map[string]interface{}); ok {
for k, v := range mapVal {
p[k] = v
}
return p, nil
}
return p, errBadPushCast
}
@cstrahan
Copy link

cstrahan commented Apr 9, 2019

You could do something like:

func castToPushType(val interface{}) (PushType, error) {
	if mapVal, ok := val.(map[string]interface{}); ok {
		return PushType(mapVal), nil
	}

	return nil, errBadPushCast
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment