func TestOptIntYAML(t *testing.T) { wrapper := OptIntWrapper{} yaml.Unmarshal([]byte("OptInt: 1"), &wrapper) assert.Equal(t, OptInt{Defined: true, Value: 1}, wrapper.OptInt) wrapper = OptIntWrapper{} yaml.Unmarshal([]byte(""), &wrapper) assert.False(t, wrapper.OptInt.Defined) wrapper = OptIntWrapper{} err := yaml.Unmarshal([]byte("OptInt: notanumber"), &wrapper) assert.Error(t, err) }
func TestOptBoolYAML(t *testing.T) { wrapper := OptBoolWrapper{} yaml.Unmarshal([]byte("OptBool: true"), &wrapper) assert.Equal(t, OptBool{Defined: true, Value: true}, wrapper.OptBool) wrapper = OptBoolWrapper{} yaml.Unmarshal([]byte("OptBool: false"), &wrapper) assert.Equal(t, OptBool{Defined: true, Value: false}, wrapper.OptBool) wrapper = OptBoolWrapper{} yaml.Unmarshal([]byte(""), &wrapper) assert.False(t, wrapper.OptBool.Defined) wrapper = OptBoolWrapper{} err := yaml.Unmarshal([]byte("OptBool: notaboolean"), &wrapper) assert.Error(t, err) }
func (s *SwaggerSchema) ValidateBytes(data []byte) error { var obj interface{} err := yaml.Unmarshal(data, &obj) if err != nil { return err } fields := obj.(map[interface{}]interface{}) apiVersion := fields["apiVersion"].(string) kind := fields["kind"].(string) return s.ValidateObject(obj, apiVersion, "", apiVersion+"."+kind) }
// LoadFromFile takes a filename and deserializes the contents into Config object func LoadFromFile(filename string) (*Config, error) { config := &Config{} kubeconfigBytes, err := ioutil.ReadFile(filename) if err != nil { return nil, err } err = yaml.Unmarshal(kubeconfigBytes, &config) if err != nil { return nil, err } return config, nil }
// unmarshal converts either JSON // or YAML into a config object. func unmarshal(data []byte, ext string) *config { var config *config var err error if ext == ".json" { err = json.Unmarshal(data, &config) } else if ext == ".yml" || ext == ".yaml" { err = yaml.Unmarshal(data, &config) } else { panic(StatusError{errors.New("Unrecognized file extension"), 65}) } if err != nil { err = displaySyntaxError(data, err) panic(StatusError{err, 65}) } return config }
func yamlToJSON(y []byte, jsonTarget *reflect.Value) ([]byte, error) { // Convert the YAML to an object. var yamlObj interface{} err := yaml.Unmarshal(y, &yamlObj) if err != nil { return nil, err } // YAML objects are not completely compatible with JSON objects (e.g. you // can have non-string keys in YAML). So, convert the YAML-compatible object // to a JSON-compatible object, failing with an error if irrecoverable // incompatibilties happen along the way. jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget) if err != nil { return nil, err } // Convert this object to JSON and return the data. return json.Marshal(jsonObj) }