func (matcher *MatchYAMLMatcher) Match(actual interface{}) (success bool, err error) { actualString, expectedString, err := matcher.prettyPrint(actual) if err != nil { return false, err } var aval interface{} var eval interface{} // this is guarded by prettyPrint candiedyaml.Unmarshal([]byte(actualString), &aval) candiedyaml.Unmarshal([]byte(expectedString), &eval) return reflect.DeepEqual(aval, eval), nil }
func TestSliceOrMapYaml(t *testing.T) { str := `{foos: [bar=baz, far=faz]}` s := StructSliceorMap{} yaml.Unmarshal([]byte(str), &s) assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s.Foos) d, err := yaml.Marshal(&s) assert.Nil(t, err) s2 := StructSliceorMap{} yaml.Unmarshal(d, &s2) assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s2.Foos) }
func readMetadata() datasource.Metadata { metadata := datasource.Metadata{} if metaDataBytes, err := ioutil.ReadFile(MetaDataFile); err == nil { yaml.Unmarshal(metaDataBytes, &metadata) } return metadata }
func (c Client) ResolveManifestVersions(yaml []byte) ([]byte, error) { m := manifest{} err := candiedyaml.Unmarshal(yaml, &m) if err != nil { return nil, err } for i, r := range m.Releases { if r.Version == "latest" { release, err := c.Release(r.Name) if err != nil { return nil, err } r.Version = release.Latest() m.Releases[i] = r } } for i, pool := range m.ResourcePools { if pool.Stemcell.Version == "latest" { stemcell, err := c.Stemcell(pool.Stemcell.Name) if err != nil { return nil, err } pool.Stemcell.Version = stemcell.Latest() m.ResourcePools[i] = pool } } return candiedyaml.Marshal(m) }
func (cfg *Config) Initialize(file []byte, authDisabled bool) error { err := candiedyaml.Unmarshal(file, &cfg) if err != nil { return err } if cfg.LogGuid == "" { return errors.New("No log_guid specified") } if !authDisabled && cfg.OAuth.TokenEndpoint == "" { return errors.New("No token endpoint specified") } if !authDisabled && cfg.OAuth.TokenEndpoint != "" && cfg.OAuth.Port == -1 { return errors.New("Routing API requires TLS enabled to get OAuth token") } err = cfg.process() if err != nil { return err } return nil }
func getServices(urls []string, key string) ([]string, error) { result := []string{} for _, url := range urls { indexUrl := fmt.Sprintf("%s/index.yml", url) content, err := LoadResource(indexUrl, true) if err != nil { log.Errorf("Failed to load %s: %v", indexUrl, err) continue } services := make(map[string][]string) err = yaml.Unmarshal(content, &services) if err != nil { log.Errorf("Failed to unmarshal %s: %v", indexUrl, err) continue } if list, ok := services[key]; ok { result = append(result, list...) } } return result, nil }
func mergeProject(p *Project, bytes []byte) (map[string]*ServiceConfig, error) { configs := make(map[string]*ServiceConfig) datas := make(rawServiceMap) if err := yaml.Unmarshal(bytes, &datas); err != nil { return nil, err } if err := interpolate(p.context.EnvironmentLookup, &datas); err != nil { return nil, err } for name, data := range datas { data, err := parse(p.context.ConfigLookup, p.context.EnvironmentLookup, p.File, data, datas) if err != nil { logrus.Errorf("Failed to parse service %s: %v", name, err) return nil, err } datas[name] = data } if err := utils.Convert(datas, &configs); err != nil { return nil, err } adjustValues(configs) return configs, nil }
func TestStringorsliceYaml(t *testing.T) { str := `{foo: [bar, baz]}` s := StructStringorslice{} yaml.Unmarshal([]byte(str), &s) assert.Equal(t, Stringorslice{"bar", "baz"}, s.Foo) d, err := yaml.Marshal(&s) assert.Nil(t, err) s2 := StructStringorslice{} yaml.Unmarshal(d, &s2) assert.Equal(t, Stringorslice{"bar", "baz"}, s2.Foo) }
func FromYAML(yaml []byte) (Manifest, error) { var m Manifest if err := candiedyaml.Unmarshal(yaml, &m); err != nil { return m, err } return m, nil }
func unmarshalOrReturnString(value string) (result interface{}) { value = strings.Replace(value, "\n", magicString, -1) if err := yaml.Unmarshal([]byte(value), &result); err != nil { result = value } result = reverseReplacement(result) return }
func TestUnmarshalCommand(t *testing.T) { s := &StructCommand{} err := yaml.Unmarshal([]byte(sampleStructCommand), s) assert.Nil(t, err) assert.Equal(t, Command{"bash"}, s.Command) assert.Nil(t, s.Entrypoint) bytes, err := yaml.Marshal(s) assert.Nil(t, err) s2 := &StructCommand{} err = yaml.Unmarshal(bytes, s2) assert.Nil(t, err) assert.Equal(t, Command{"bash"}, s2.Command) assert.Nil(t, s2.Entrypoint) }
func TestUnmarshalEmptyCommand(t *testing.T) { s := &StructCommand{} err := yaml.Unmarshal([]byte(sampleEmptyCommand), s) assert.Nil(t, err) assert.Nil(t, s.Command) bytes, err := yaml.Marshal(s) assert.Nil(t, err) assert.Equal(t, "{}", strings.TrimSpace(string(bytes))) s2 := &StructCommand{} err = yaml.Unmarshal(bytes, s2) assert.Nil(t, err) assert.Nil(t, s2.Command) }
func TestUnmarshalSlice(t *testing.T) { expected := &structStringorslice{Foo: NewStringorslice("a", "b", "c")} b := []byte("foo: [a, b, c]\n") s := &structStringorslice{} err := yaml.Unmarshal(b, s) assert.Equal(t, expected, s) assert.Nil(t, err) }
func TestUnmarshalEmpty(t *testing.T) { expected := &structStringorslice{Foo: NewStringorslice()} b := []byte("{}\n") s := &structStringorslice{} err := yaml.Unmarshal(b, s) assert.Equal(t, expected, s) assert.Nil(t, err) }
func testInvalidInterpolatedConfig(t *testing.T, interpolatedConfig string) { interpolatedConfigBytes := []byte(interpolatedConfig) interpolatedData := make(RawServiceMap) yaml.Unmarshal(interpolatedConfigBytes, &interpolatedData) err := Interpolate(new(MockEnvironmentLookup), &interpolatedData) assert.NotNil(t, err) }
func Convert(from, to interface{}) error { bytes, err := yaml.Marshal(from) if err != nil { log.WithFields(log.Fields{"from": from, "err": err}).Warn("Error serializing to YML") return err } return yaml.Unmarshal(bytes, to) }
func parseBody(body []byte) (*Images, error) { update := &Images{} err := yaml.Unmarshal(body, update) if err != nil { return nil, err } return update, nil }
func (c *Config) initConfigFromFile(path string) error { var e error b, e := ioutil.ReadFile(path) if e != nil { return e } return candiedyaml.Unmarshal(b, &c) }
func Parse(sourceName string, source []byte) (Node, error) { var parsed interface{} err := candiedyaml.Unmarshal(source, &parsed) if err != nil { return nil, err } return sanitize(sourceName, parsed) }
func TestMaporsliceYaml(t *testing.T) { str := `{foo: {bar: baz, far: 1}}` s := StructMaporslice{} yaml.Unmarshal([]byte(str), &s) assert.Equal(t, 2, len(s.Foo)) assert.True(t, contains(s.Foo, "bar=baz")) assert.True(t, contains(s.Foo, "far=1")) d, err := yaml.Marshal(&s) assert.Nil(t, err) s2 := StructMaporslice{} yaml.Unmarshal(d, &s2) assert.Equal(t, 2, len(s2.Foo)) assert.True(t, contains(s2.Foo, "bar=baz")) assert.True(t, contains(s2.Foo, "far=1")) }
func (c Client) ScanAndFix(yaml []byte) error { var manifest struct { Name string Jobs []struct { Name string Instances int } } err := candiedyaml.Unmarshal(yaml, &manifest) if err != nil { return err } jobs := make(map[string][]int) for _, j := range manifest.Jobs { if j.Instances > 0 { var indices []int for i := 0; i < j.Instances; i++ { indices = append(indices, i) } jobs[j.Name] = indices } } requestBody, err := json.Marshal(map[string]interface{}{ "jobs": jobs, }) if err != nil { return err } request, err := http.NewRequest("PUT", fmt.Sprintf("%s/deployments/%s/scan_and_fix", c.config.URL, manifest.Name), bytes.NewBuffer(requestBody)) if err != nil { return err } request.SetBasicAuth(c.config.Username, c.config.Password) request.Header.Set("Content-Type", "application/json") response, err := transport.RoundTrip(request) if err != nil { return err } if response.StatusCode != http.StatusFound { return fmt.Errorf("unexpected response %d %s", response.StatusCode, http.StatusText(response.StatusCode)) } err = c.checkTask(response.Header.Get("Location")) if err != nil { return err } return nil }
func TestMarshalConfig(t *testing.T) { config := newTestConfig() bytes, err := yaml.Marshal(config) assert.Nil(t, err) config2 := TestConfig{} err = yaml.Unmarshal(bytes, &config2) assert.Nil(t, err) assert.Equal(t, config, config2) }
// Convert converts a struct (src) to another one (target) using yaml marshalling/unmarshalling. // If the structure are not compatible, this will throw an error as the unmarshalling will fail. func Convert(src, target interface{}) error { newBytes, err := yaml.Marshal(src) if err != nil { return err } err = yaml.Unmarshal(newBytes, target) if err != nil { logrus.Errorf("Failed to unmarshall: %v\n%s", err, string(newBytes)) } return err }
func TestMarshalServiceConfig(t *testing.T) { configPtr := newTestConfig().SystemContainers["udev"] bytes, err := yaml.Marshal(configPtr) assert.Nil(t, err) configPtr2 := &ServiceConfig{} err = yaml.Unmarshal(bytes, configPtr2) assert.Nil(t, err) assert.Equal(t, configPtr, configPtr2) }
func readConfig(bytes []byte, substituteMetadataVars bool, files ...string) (map[interface{}]interface{}, error) { // You can't just overlay yaml bytes on to maps, it won't merge, but instead // just override the keys and not merge the map values. left := make(map[interface{}]interface{}) metadata := readMetadata() for _, file := range files { content, err := readConfigFile(file) if err != nil { return nil, err } if len(content) == 0 { continue } if substituteMetadataVars { content = substituteVars(content, metadata) } right := make(map[interface{}]interface{}) err = yaml.Unmarshal(content, &right) if err != nil { return nil, err } left = util.MapsUnion(left, right) } if bytes != nil && len(bytes) > 0 { right := make(map[interface{}]interface{}) if substituteMetadataVars { bytes = substituteVars(bytes, metadata) } if err := yaml.Unmarshal(bytes, &right); err != nil { return nil, err } left = util.MapsUnion(left, right) } return left, nil }
func composeToCloudConfig(bytes []byte) ([]byte, error) { compose := make(map[interface{}]interface{}) err := yaml.Unmarshal(bytes, &compose) if err != nil { return nil, err } return yaml.Marshal(map[interface{}]interface{}{ "rancher": map[interface{}]interface{}{ "services": compose, }, }) }
func testInterpolatedConfig(t *testing.T, expectedConfig, interpolatedConfig string, envVariables map[string]string) { for k, v := range envVariables { os.Setenv(k, v) } expectedConfigBytes := []byte(expectedConfig) interpolatedConfigBytes := []byte(interpolatedConfig) expectedData := make(RawServiceMap) interpolatedData := make(RawServiceMap) yaml.Unmarshal(expectedConfigBytes, &expectedData) yaml.Unmarshal(interpolatedConfigBytes, &interpolatedData) _ = Interpolate(MockEnvironmentLookup{envVariables}, &interpolatedData) for k := range envVariables { os.Unsetenv(k) } assert.Equal(t, expectedData, interpolatedData) }
func TestStr2SliceOrMapPtrMap(t *testing.T) { s := map[string]*StructSliceorMap{"udav": { Foos: SliceorMap{"io.rancher.os.bar": "baz", "io.rancher.os.far": "true"}, Bars: []string{}, }} d, err := yaml.Marshal(&s) assert.Nil(t, err) s2 := map[string]*StructSliceorMap{} yaml.Unmarshal(d, &s2) assert.Equal(t, s, s2) }
func (matcher *MatchYAMLMatcher) prettyPrint(actual interface{}) (actualFormatted, expectedFormatted string, err error) { actualString, aok := toString(actual) expectedString, eok := toString(matcher.YAMLToMatch) if !(aok && eok) { return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1)) } var adata interface{} if err := candiedyaml.Unmarshal([]byte(actualString), &adata); err != nil { return "", "", err } abuf, _ := candiedyaml.Marshal(adata) var edata interface{} if err := candiedyaml.Unmarshal([]byte(expectedString), &edata); err != nil { return "", "", err } ebuf, _ := candiedyaml.Marshal(edata) return string(abuf), string(ebuf), nil }
// MergeServices merges a compose file into an existing set of service configs func MergeServices(existingServices *Configs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte) (map[string]*ServiceConfig, error) { configs := make(map[string]*ServiceConfig) datas := make(RawServiceMap) if err := yaml.Unmarshal(bytes, &datas); err != nil { return nil, err } if err := Interpolate(environmentLookup, &datas); err != nil { return nil, err } if err := validate(datas); err != nil { return nil, err } for name, data := range datas { data, err := parse(resourceLookup, environmentLookup, file, data, datas) if err != nil { logrus.Errorf("Failed to parse service %s: %v", name, err) return nil, err } if serviceConfig, ok := existingServices.Get(name); ok { var rawExistingService RawService if err := utils.Convert(serviceConfig, &rawExistingService); err != nil { return nil, err } data = mergeConfig(rawExistingService, data) } datas[name] = data } for name, data := range datas { err := validateServiceConstraints(data, name) if err != nil { return nil, err } } if err := utils.Convert(datas, &configs); err != nil { return nil, err } adjustValues(configs) return configs, nil }