Last active
September 12, 2018 10:01
-
-
Save ninedraft/9f3e10776e7a116ad371049a4f5e614c to your computer and use it in GitHub Desktop.
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 kube | |
import ( | |
"encoding/json" | |
"io" | |
"github.com/ericchiang/k8s" | |
metav1 "github.com/ericchiang/k8s/apis/meta/v1" | |
"github.com/mitchellh/mapstructure" | |
"github.com/thoas/go-funk" | |
"gopkg.in/yaml.v2" | |
) | |
var ( | |
_ json.Marshaler = Object{} | |
_ json.Unmarshaler = new(Object) | |
_ yaml.Marshaler = Object{} | |
_ yaml.Unmarshaler = new(Object) | |
_ k8s.Resource = Object{} | |
) | |
type Object struct { | |
meta *metav1.ObjectMeta | |
body map[string]interface{} | |
} | |
func (object Object) Query(path string) interface{} { | |
return funk.Get(object.body, path) | |
} | |
func ObjectFromJSON(re io.Reader) (Object, error) { | |
var object = Object{ | |
body: make(map[string]interface{}), | |
} | |
return object, json.NewDecoder(re).Decode(object.body) | |
} | |
func ObjectFromYAML(re io.Reader) (Object, error) { | |
var object = Object{ | |
body: make(map[string]interface{}), | |
} | |
return object, yaml.NewDecoder(re).Decode(object.body) | |
} | |
func ObjectFromStruct(data interface{}) (Object, error) { | |
var object Object | |
return object, mapstructure.Decode(data, &object.body) | |
} | |
func (object Object) initMeta() { | |
if object.meta == nil { | |
object.meta = new(metav1.ObjectMeta) | |
} | |
mapstructure.WeakDecode(object.body, object.meta) | |
} | |
func (object Object) GetMetadata() *metav1.ObjectMeta { | |
if object.meta == nil { | |
object.initMeta() | |
} | |
return object.meta | |
} | |
func (object Object) MarshalJSON() ([]byte, error) { | |
return json.Marshal(object.body) | |
} | |
func (object *Object) UnmarshalJSON(data []byte) error { | |
return yaml.Unmarshal(data, &object.body) | |
} | |
func (object Object) MarshalYAML() (interface{}, error) { | |
return object.body, nil | |
} | |
func (object *Object) UnmarshalYAML(unmarshaler func(interface{}) error) error { | |
return unmarshaler(object.body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment