add yaml convertor
[msb/service-mesh.git] / msb2pilot / src / msb2pilot / util / yaml.go
1 /**
2  * Copyright (c) 2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial Project
11  */
12 package util
13
14 import (
15         "gopkg.in/yaml.v2"
16 )
17
18 /**
19 * type T struct {
20 *   F int `yaml:"a, omitempty"`
21 *   B int
22 * }
23 * var t T
24 * yaml.Marshal(&T{B:2})   /// returns "b: 2 \n"
25 * yaml.Marshal(&T{F:1})   // returns "a: 1 \nb: 0\n"
26  */
27 func MarshalYaml(in interface{}) (string, error) {
28         b, err := yaml.Marshal(in)
29         if err != nil {
30                 return "", err
31         } else {
32                 return string(b), nil
33         }
34 }
35
36 /**
37 * var t T
38 * yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
39 *
40  */
41 func UnmarshalYaml(str string, out interface{}) error {
42         return yaml.Unmarshal([]byte(str), out)
43 }