本文整理匯總了Golang中github.com/ghodss/yaml.Marshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Marshal函數的具體用法?Golang Marshal怎麽用?Golang Marshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Marshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SetCredential
// SetCredential sets a credential by name.
func (scp *SecretsCredentialProvider) SetCredential(name string, credential *Credential) error {
// Marshal the credential & base64 encode it.
b, err := yaml.Marshal(credential)
if err != nil {
log.Printf("yaml marshal failed for credential named %s: %s", name, err)
return err
}
enc := base64.StdEncoding.EncodeToString(b)
// Then create a kubernetes object out of it
metadata := make(map[string]string)
metadata["name"] = name
data := make(map[string]string)
data["credential"] = enc
obj := &util.KubernetesSecret{
Kind: secretType,
APIVersion: "v1",
Metadata: metadata,
Data: data,
}
ko, err := yaml.Marshal(obj)
if err != nil {
log.Printf("yaml marshal failed for kubernetes object named %s: %s", name, err)
return err
}
_, err = scp.k.Create(string(ko))
return err
}
示例2: RunController
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
func RunController(ctx api.Context, image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
// TODO replace ctx with a namespace string
if servicePort > 0 && !util.IsDNSLabel(name) {
return fmt.Errorf("service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
}
ports, err := portsFromString(portSpec)
if err != nil {
return err
}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: name,
},
Spec: api.ReplicationControllerSpec{
Replicas: replicas,
Selector: map[string]string{
"name": name,
},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": name,
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: strings.ToLower(name),
Image: image,
Ports: ports,
},
},
},
},
},
}
controllerOut, err := client.ReplicationControllers(api.Namespace(ctx)).Create(controller)
if err != nil {
return err
}
data, err := yaml.Marshal(controllerOut)
if err != nil {
return err
}
fmt.Print(string(data))
if servicePort > 0 {
svc, err := createService(ctx, name, servicePort, client)
if err != nil {
return err
}
data, err = yaml.Marshal(svc)
if err != nil {
return err
}
fmt.Printf(string(data))
}
return nil
}
示例3: Write
func (c *Config) Write() error {
b, err := yaml.Marshal(c)
if err != nil {
return err
}
err = ioutil.WriteFile(saferc(), b, 0600)
if err != nil {
return err
}
url, token, skipverify, err := c.credentials()
if err != nil {
return err
}
b, err = yaml.Marshal(
struct {
URL string `json:"vault"`
Token string `json:"token"`
SkipVerify bool `json:"skip_verify"`
}{url, token, skipverify})
if err != nil {
return err
}
return ioutil.WriteFile(svtoken(), b, 0600)
}
示例4: TestParseInto
func TestParseInto(t *testing.T) {
got := map[string]interface{}{
"outer": map[string]interface{}{
"inner1": "overwrite",
"inner2": "value2",
},
}
input := "outer.inner1=value1,outer.inner3=value3"
expect := map[string]interface{}{
"outer": map[string]interface{}{
"inner1": "value1",
"inner2": "value2",
"inner3": "value3",
},
}
if err := ParseInto(input, got); err != nil {
t.Fatal(err)
}
y1, err := yaml.Marshal(expect)
if err != nil {
t.Fatal(err)
}
y2, err := yaml.Marshal(got)
if err != nil {
t.Fatalf("Error serializing parsed value: %s", err)
}
if string(y1) != string(y2) {
t.Errorf("%s: Expected:\n%s\nGot:\n%s", input, y1, y2)
}
}
示例5: SetCredential
func (scp *SecretsCredentialProvider) SetCredential(name string, credential *common.RegistryCredential) error {
// Marshal the credential & base64 encode it.
b, err := yaml.Marshal(credential)
if err != nil {
log.Printf("yaml marshal failed for credential: %s: %v", name, err)
return err
}
enc := base64.StdEncoding.EncodeToString(b)
// Then create a kubernetes object out of it
metadata := make(map[string]string)
metadata["name"] = name
data := make(map[string]string)
data["credential"] = enc
obj := &common.KubernetesSecret{
Kind: secretType,
ApiVersion: "v1",
Metadata: metadata,
Data: data,
}
ko, err := yaml.Marshal(obj)
if err != nil {
log.Printf("yaml marshal failed for kubernetes object: %s: %v", name, err)
return err
}
log.Printf("Calling with: %s", string(ko))
o, err := scp.k.Create(string(ko))
log.Printf("Create returned: %s", o)
return err
}
示例6: SaveChartfile
// SaveChartfile saves the given metadata as a Chart.yaml file at the given path.
//
// 'filename' should be the complete path and filename ('foo/Chart.yaml')
func SaveChartfile(filename string, cf *chart.Metadata) error {
out, err := yaml.Marshal(cf)
if err != nil {
return err
}
return ioutil.WriteFile(filename, out, 0755)
}
示例7: callServiceWithConfiguration
func (d *deployer) callServiceWithConfiguration(method, operation string, configuration *common.Configuration) (*common.Configuration, error) {
callback := func(e error) error {
return fmt.Errorf("cannot %s configuration: %s", operation, e)
}
y, err := yaml.Marshal(configuration)
if err != nil {
return nil, callback(err)
}
reader := ioutil.NopCloser(bytes.NewReader(y))
resp, err := d.callService(method, d.getBaseURL(), reader, callback)
if err != nil {
return nil, err
}
result := &common.Configuration{}
if len(resp) != 0 {
if err := yaml.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("cannot unmarshal response: (%v)", err)
}
}
return result, nil
}
示例8: Reindex
// Reindex adds an entry to the index file at the given path
func Reindex(ch *chart.Chart, path string) error {
name := ch.Metadata.Name + "-" + ch.Metadata.Version
y, err := LoadIndexFile(path)
if err != nil {
return err
}
found := false
for k := range y.Entries {
if k == name {
found = true
break
}
}
if !found {
dig, err := provenance.DigestFile(path)
if err != nil {
return err
}
y.Add(ch.Metadata, name+".tgz", "http://localhost:8879/charts", "sha256:"+dig)
out, err := yaml.Marshal(y)
if err != nil {
return err
}
ioutil.WriteFile(path, out, 0644)
}
return nil
}
示例9: DoParseTest
func DoParseTest(t *testing.T, storage string, obj runtime.Object, codec runtime.Codec, p *Parser) {
jsonData, _ := codec.Encode(obj)
var tmp map[string]interface{}
json.Unmarshal(jsonData, &tmp)
yamlData, _ := yaml.Marshal(tmp)
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
t.Logf("Intermediate json:\n%v\n", string(jsonData))
jsonGot, jsonErr := p.ToWireFormat(jsonData, storage, latest.Codec, codec)
yamlGot, yamlErr := p.ToWireFormat(yamlData, storage, latest.Codec, codec)
if jsonErr != nil {
t.Errorf("json err: %#v", jsonErr)
}
if yamlErr != nil {
t.Errorf("yaml err: %#v", yamlErr)
}
if string(jsonGot) != string(jsonData) {
t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
string(jsonGot), string(jsonData))
}
if string(yamlGot) != string(jsonData) {
t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
string(yamlGot), string(jsonData))
}
}
示例10: writeConfig
// writeConfig
func writeConfig(path string, v interface{}) error {
// take a config objects path and create (and truncate) the file, preparing it
// to receive new configurations
f, err := os.Create(path)
if err != nil {
Fatal("[config/config] os.Create() failed", err.Error())
}
defer f.Close()
// marshal the config object
b, err := yaml.Marshal(v)
if err != nil {
Fatal("[config/config] yaml.Marshal() failed", err.Error())
}
// mutex.Lock()
// write it back to the file
if _, err := f.Write(b); err != nil {
return err
}
// mutex.Unlock()
return nil
}
示例11: TestValidateSchema
func TestValidateSchema(t *testing.T) {
Convey("Test valid schema", t, func() {
config := testConfig{"Tom", "Justin"}
cfgValidator = &schemaValidatorType{}
jb, _ := json.Marshal(config)
errs := ValidateSchema(MOCK_CONSTRAINTS, string(jb))
So(errs, ShouldBeNil)
})
Convey("Test invalid schema", t, func() {
config := testConfig{"Tom", "Justin"}
cfgValidator = &schemaValidatorType{}
jb, _ := json.Marshal(config)
errs := ValidateSchema(INVALID_MOCK_CONSTRAINTS, string(jb))
So(errs, ShouldNotBeNil)
})
Convey("Test invalid json", t, func() {
config := testConfig{"Tom", "Justin"}
cfgValidator = &schemaValidatorType{}
yb, _ := yaml.Marshal(config)
errs := ValidateSchema(MOCK_CONSTRAINTS, string(yb))
So(errs, ShouldNotBeNil)
})
}
示例12: getDependencies
// getDependencies iterates over resources and returns a map of resource name to
// the set of dependencies that resource has.
//
// Dependencies are reversed for delete operation.
func getDependencies(c *common.Configuration, o operation) (DependencyMap, error) {
deps := DependencyMap{}
// Prepopulate map. This will be used later to validate referenced resources
// actually exist.
for _, r := range c.Resources {
deps[r.Name] = make(map[string]bool)
}
for _, r := range c.Resources {
props, err := yaml.Marshal(r.Properties)
if err != nil {
return nil, fmt.Errorf("Failed to deserialize resource properties for resource %s: %v", r.Name, r.Properties)
}
refs := refRe.FindAllStringSubmatch(string(props), -1)
for _, ref := range refs {
// Validate referenced resource exists in config.
if _, ok := deps[ref[1]]; !ok {
return nil, fmt.Errorf("Invalid resource name in reference: %s", ref[1])
}
// Delete dependencies should be reverse of create.
if o == DeleteOperation {
deps[ref[1]][r.Name] = true
} else {
deps[r.Name][ref[1]] = true
}
}
}
return deps, nil
}
示例13: toYAML
func toYAML(v interface{}) string {
y, err := yaml.Marshal(v)
if err != nil {
panic(fmt.Sprintf("yaml marshal failed: %v", err))
}
return string(y)
}
示例14: WriteFile
// WriteFile writes a repositories file to the given path.
func (r *RepoFile) WriteFile(path string, perm os.FileMode) error {
data, err := yaml.Marshal(r)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, perm)
}
示例15: generateReplicationController
func generateReplicationController(name string, image string, replicas int, ports []v1beta1.Port) {
controller := []v1beta1.ReplicationController{{
TypeMeta: v1beta1.TypeMeta{APIVersion: "v1beta1", Kind: "ReplicationController", ID: name},
DesiredState: v1beta1.ReplicationControllerState{
Replicas: replicas,
ReplicaSelector: map[string]string{
"simpleservice": name,
},
PodTemplate: v1beta1.PodTemplate{
DesiredState: v1beta1.PodState{
Manifest: v1beta1.ContainerManifest{
Version: "v1beta2",
Containers: []v1beta1.Container{
{
Name: name,
Image: image,
Ports: ports,
},
},
},
},
Labels: map[string]string{
"simpleservice": name,
},
},
},
Labels: map[string]string{
"simpleservice": name,
},
}}
controllerOutData, err := yaml.Marshal(controller)
checkErr(err)
fmt.Print(string(controllerOutData))
}