本文整理汇总了Golang中gopkg/in/v1/yaml.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestIntOrStringMarshalYAML
func TestIntOrStringMarshalYAML(t *testing.T) {
{
input := IntOrStringHolder{
IOrS: IntOrString{
Kind: IntstrInt,
IntVal: 123,
},
}
result, err := yaml.Marshal(&input)
if err != nil {
t.Errorf("Failed to marshal: %v", err)
}
if string(result) != "val: 123\n" {
t.Errorf("Failed to marshal int-typed IntOrString: %q", string(result))
}
}
{
input := IntOrStringHolder{
IOrS: IntOrString{
Kind: IntstrString,
StrVal: "123",
},
}
result, err := yaml.Marshal(&input)
if err != nil {
t.Errorf("Failed to marshal: %v", err)
}
if string(result) != "val: \"123\"\n" {
t.Errorf("Failed to marshal string-typed IntOrString: %q", string(result))
}
}
}
示例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: RunController
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'
func RunController(image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
controller := api.ReplicationController{
JSONBase: api.JSONBase{
ID: name,
},
DesiredState: api.ReplicationControllerState{
Replicas: replicas,
ReplicaSelector: map[string]string{
"name": name,
},
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta2",
Containers: []api.Container{
{
Name: strings.ToLower(name),
Image: image,
Ports: makePorts(portSpec),
},
},
},
},
Labels: map[string]string{
"name": name,
},
},
},
Labels: map[string]string{
"name": name,
},
}
controllerOut, err := client.CreateReplicationController(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(name, servicePort, client)
if err != nil {
return err
}
data, err = yaml.Marshal(svc)
if err != nil {
return err
}
fmt.Printf(string(data))
}
return nil
}
示例4: RunController
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
func RunController(image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
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)
}
controller := &api.ReplicationController{
JSONBase: api.JSONBase{
ID: name,
},
DesiredState: api.ReplicationControllerState{
Replicas: replicas,
ReplicaSelector: map[string]string{
"replicationController": name,
},
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta2",
Containers: []api.Container{
{
Name: strings.ToLower(name),
Image: image,
Ports: portsFromString(portSpec),
},
},
},
},
Labels: map[string]string{
"replicationController": name,
},
},
},
}
controllerOut, err := client.CreateReplicationController(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(name, servicePort, client)
if err != nil {
return err
}
data, err = yaml.Marshal(svc)
if err != nil {
return err
}
fmt.Printf(string(data))
}
return nil
}
示例5: TestMarshalTypeCache
func (s *S) TestMarshalTypeCache(c *C) {
var data []byte
var err error
func() {
type T struct{ A int }
data, err = yaml.Marshal(&T{})
c.Assert(err, IsNil)
}()
func() {
type T struct{ B int }
data, err = yaml.Marshal(&T{})
c.Assert(err, IsNil)
}()
c.Assert(string(data), Equals, "b: 0\n")
}
示例6: TestMarshal
func (s *S) TestMarshal(c *C) {
for _, item := range marshalTests {
data, err := yaml.Marshal(item.value)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, item.data)
}
}
示例7: 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))
}
}
示例8: main
func main() {
if len(os.Args) != 3 {
checkErr(fmt.Errorf(usage))
}
specFilename := os.Args[1]
configFilename := os.Args[2]
specData, err := ReadConfigData(specFilename)
checkErr(err)
spec := EnscopeSpec{}
err = yaml.Unmarshal(specData, &spec)
checkErr(err)
configData, err := ReadConfigData(configFilename)
checkErr(err)
var data interface{}
err = yaml.Unmarshal([]byte(configData), &data)
checkErr(err)
xData, err := enscope("", spec, data)
checkErr(err)
out, err := yaml.Marshal(xData)
checkErr(err)
fmt.Print(string(out))
}
示例9: generateReplicationController
func generateReplicationController(sname string, cname string, replicas int) {
if replicas < 1 {
replicas = 1
}
name := fmt.Sprintf("%s-%s", sname, cname)
rc := []v1beta3.ReplicationController{{
TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "ReplicationController"},
ObjectMeta: v1beta3.ObjectMeta{
Name: name,
Labels: map[string]string{
"service": sname,
"track": cname,
},
},
Spec: v1beta3.ReplicationControllerSpec{
Replicas: replicas,
Selector: map[string]string{
"service": sname,
"track": cname,
},
Template: v1beta3.ObjectReference{
Kind: "PodTemplate",
Name: name,
APIVersion: "v1beta3",
},
},
}}
rcOutData, err := yaml.Marshal(rc)
checkErr(err)
fmt.Print(string(rcOutData))
}
示例10: generatePodTemplate
func generatePodTemplate(sname string, cname string, podSpec v1beta3.PodSpec) {
name := fmt.Sprintf("%s-%s", sname, cname)
pt := []v1beta3.PodTemplate{{
TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "PodTemplate"},
ObjectMeta: v1beta3.ObjectMeta{
Name: name,
Labels: map[string]string{
"service": sname,
"track": cname,
},
},
Spec: v1beta3.PodTemplateSpec{
ObjectMeta: v1beta3.ObjectMeta{
Labels: map[string]string{
"service": sname,
"track": cname,
},
},
Spec: podSpec,
},
}}
ptOutData, err := yaml.Marshal(pt)
checkErr(err)
fmt.Print(string(ptOutData))
}
示例11: generateService
func generateService(name string, portSpec string) {
if portSpec == "" {
return
}
servicePort, containerPort, err := portsFromString(portSpec)
checkErr(err)
svc := []v1beta3.Service{{
TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "Service"},
ObjectMeta: v1beta3.ObjectMeta{
Name: name,
Labels: map[string]string{
"service": name,
},
},
Spec: v1beta3.ServiceSpec{
Port: servicePort,
ContainerPort: util.NewIntOrStringFromInt(containerPort),
Selector: map[string]string{
"service": name,
},
},
}}
svcOutData, err := yaml.Marshal(svc)
checkErr(err)
fmt.Print(string(svcOutData))
}
示例12: 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))
}
示例13: ReMarshal
// ReMarshal parses interface{} into concrete types
func ReMarshal(config interface{}, target interface{}) error {
data, err := yaml.Marshal(config)
if err != nil {
return err
}
return yaml.Unmarshal(data, target)
}
示例14: TestStatus
func TestStatus(t *testing.T) {
mc, mcli, f := fixture()
mc.Err, mc.P = make([]error, 3), []string{"LM-X"}
f.Build = 1
out, err := mcli.Status()
mc.Check(t)
if out == nil || len(out) != 1 {
t.Error("expected out to not be empty")
}
if err != nil && len(err) != 0 {
t.Error("expected err to be empty")
}
y, e := yaml.Marshal(map[string][]pulse.BuildResult{fmt.Sprintf("%s (build 1)", mc.P[0]): {}})
if e != nil {
t.Error("expected err to be nil")
}
s, ok := out[0].(string)
if !ok {
t.Fatalf("expected out[0] to be of string type, was %T instead", out[0])
}
if string(y) != s {
t.Errorf("expected %s, got %s", y, out[0])
}
}
示例15: PrintObj
// PrintObj prints the data as YAML.
func (y *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
output, err := yaml.Marshal(obj)
if err != nil {
return err
}
_, err = fmt.Fprint(w, string(output))
return err
}