本文整理匯總了Golang中github.com/cloudfoundry-incubator/candiedyaml.Marshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Marshal函數的具體用法?Golang Marshal怎麽用?Golang Marshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Marshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: diff
func diff(aFilePath, bFilePath string, separator string) {
aFile, err := ioutil.ReadFile(aFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading a [%s]:", path.Clean(aFilePath)), err)
}
aYAML, err := yaml.Parse(aFilePath, aFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing a [%s]:", path.Clean(aFilePath)), err)
}
bFile, err := ioutil.ReadFile(bFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading b [%s]:", path.Clean(bFilePath)), err)
}
bYAML, err := yaml.Parse(bFilePath, bFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing b [%s]:", path.Clean(bFilePath)), err)
}
diffs := compare.Compare(aYAML, bYAML)
if len(diffs) == 0 {
fmt.Println("no differences!")
return
}
for _, diff := range diffs {
fmt.Println("Difference in", strings.Join(diff.Path, "."))
if diff.A != nil {
ayaml, err := candiedyaml.Marshal(diff.A)
if err != nil {
panic(err)
}
fmt.Printf(" %s has:\n \x1b[31m%s\x1b[0m\n", aFilePath, strings.Replace(string(ayaml), "\n", "\n ", -1))
}
if diff.B != nil {
byaml, err := candiedyaml.Marshal(diff.B)
if err != nil {
panic(err)
}
fmt.Printf(" %s has:\n \x1b[32m%s\x1b[0m\n", bFilePath, strings.Replace(string(byaml), "\n", "\n ", -1))
}
fmt.Printf(separator)
}
}
示例2: composeUp
// composeUp converts given json to yaml, saves to a file on the host and
// uses `docker-compose up -d` to create the containers.
func composeUp(d driver.DistroDriver, json map[string]interface{}) error {
if len(json) == 0 {
log.Println("docker-compose config not specified, noop")
return nil
}
// Convert json to yaml
yaml, err := yaml.Marshal(json)
if err != nil {
return fmt.Errorf("error converting to compose.yml: %v", err)
}
if err := os.MkdirAll(composeYmlDir, 0777); err != nil {
return fmt.Errorf("failed creating %s: %v", composeYmlDir, err)
}
log.Printf("Using compose yaml:>>>>>\n%s\n<<<<<", string(yaml))
ymlPath := filepath.Join(composeYmlDir, composeYml)
if err := ioutil.WriteFile(ymlPath, yaml, 0666); err != nil {
return fmt.Errorf("error writing %s: %v", ymlPath, err)
}
// set timeout for docker-compose -> docker-engine interactions.
// When downloading large images, docker-compose intermittently times out
// (gh#docker/compose/issues/2186).
os.Setenv("COMPOSE_HTTP_TIMEOUT", fmt.Sprintf("%d", composeTimeoutSecs)) // versions <= 1.4.2
os.Setenv("DOCKER_CLIENT_TIMEOUT", fmt.Sprintf("%d", composeTimeoutSecs)) // version >= 1.5.0
defer os.Unsetenv("COMPOSE_HTTP_TIMEOUT")
defer os.Unsetenv("DOCKER_CLIENT_TIMEOUT")
return executil.ExecPipeToFds(executil.Fds{Out: ioutil.Discard}, composeBinPath(d), "-p", composeProject, "-f", ymlPath, "up", "-d")
}
示例3: configGet
func configGet(c *cli.Context) {
arg := c.Args().Get(0)
if arg == "" {
return
}
cfg, err := config.LoadConfig()
if err != nil {
log.WithFields(log.Fields{"err": err}).Fatal("config get: failed to load config")
}
val, err := cfg.Get(arg)
if err != nil {
log.WithFields(log.Fields{"cfg": cfg, "key": arg, "val": val, "err": err}).Fatal("config get: failed to retrieve value")
}
printYaml := false
switch val.(type) {
case []interface{}:
printYaml = true
case map[interface{}]interface{}:
printYaml = true
}
if printYaml {
bytes, err := yaml.Marshal(val)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(bytes))
} else {
fmt.Println(val)
}
}
示例4: saveFiles
func saveFiles(cloudConfigBytes, scriptBytes []byte, metadata datasource.Metadata) error {
os.MkdirAll(rancherConfig.CloudConfigDir, os.ModeDir|0600)
os.Remove(rancherConfig.CloudConfigScriptFile)
os.Remove(rancherConfig.CloudConfigBootFile)
os.Remove(rancherConfig.MetaDataFile)
if len(scriptBytes) > 0 {
log.Infof("Writing to %s", rancherConfig.CloudConfigScriptFile)
if err := ioutil.WriteFile(rancherConfig.CloudConfigScriptFile, scriptBytes, 500); err != nil {
log.Errorf("Error while writing file %s: %v", rancherConfig.CloudConfigScriptFile, err)
return err
}
}
if err := ioutil.WriteFile(rancherConfig.CloudConfigBootFile, cloudConfigBytes, 400); err != nil {
return err
}
log.Infof("Written to %s:\n%s", rancherConfig.CloudConfigBootFile, string(cloudConfigBytes))
metaDataBytes, err := yaml.Marshal(metadata)
if err != nil {
return err
}
if err = ioutil.WriteFile(rancherConfig.MetaDataFile, metaDataBytes, 400); err != nil {
return err
}
log.Infof("Written to %s:\n%s", rancherConfig.MetaDataFile, string(metaDataBytes))
return nil
}
示例5: ResolveManifestVersions
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)
}
示例6: Dump
func Dump(boot, private, full bool) (string, error) {
var cfg *CloudConfig
var err error
if full {
cfg, err = LoadConfig()
} else {
files := []string{CloudConfigBootFile, CloudConfigPrivateFile, CloudConfigFile}
if !private {
files = util.FilterStrings(files, func(x string) bool { return x != CloudConfigPrivateFile })
}
if !boot {
files = util.FilterStrings(files, func(x string) bool { return x != CloudConfigBootFile })
}
cfg, err = ChainCfgFuncs(nil,
func(_ *CloudConfig) (*CloudConfig, error) { return ReadConfig(nil, true, files...) },
amendNils,
)
}
if err != nil {
return "", err
}
bytes, err := yaml.Marshal(*cfg)
return string(bytes), err
}
示例7: formatYAML
func formatYAML(yaml yaml.Node) string {
formatted, err := candiedyaml.Marshal(yaml)
if err != nil {
return fmt.Sprintf("\n\t<%T> %#v", yaml, yaml)
}
return fmt.Sprintf("\n\t%s", strings.Replace(string(formatted), "\n", "\n\t", -1))
}
示例8: WriteToFile
func WriteToFile(data interface{}, filename string) error {
content, err := yaml.Marshal(data)
if err != nil {
return err
}
return ioutil.WriteFile(filename, content, 400)
}
示例9: Convert
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)
}
示例10: Convert
// 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
}
示例11: TestMarshalConfig
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)
}
示例12: TestMarshalServiceConfig
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)
}
示例13: TestStr2SliceOrMapPtrMap
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)
}
示例14: composeToCloudConfig
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,
},
})
}
示例15: prettyPrint
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
}