本文整理汇总了Golang中github.com/ghodss/yaml.Unmarshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Unmarshal函数的具体用法?Golang Unmarshal怎么用?Golang Unmarshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Unmarshal函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: LoadRepositoriesFile
// LoadRepositoriesFile takes a file at the given path and returns a RepoFile object
//
// If this returns ErrRepoOutOfDate, it also returns a recovered RepoFile that
// can be saved as a replacement to the out of date file.
func LoadRepositoriesFile(path string) (*RepoFile, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
r := &RepoFile{}
err = yaml.Unmarshal(b, r)
if err != nil {
return nil, err
}
// File is either corrupt, or is from before v2.0.0-Alpha.5
if r.APIVersion == "" {
m := map[string]string{}
if err = yaml.Unmarshal(b, &m); err != nil {
return nil, err
}
r := NewRepoFile()
for k, v := range m {
r.Add(&Entry{
Name: k,
URL: v,
Cache: fmt.Sprintf("%s-index.yaml", k),
})
}
return r, ErrRepoOutOfDate
}
return r, nil
}
开发者ID:technosophos,项目名称:k8s-helm,代码行数:35,代码来源:repo.go
示例2: 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))
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:30,代码来源:enscope.go
示例3: deployerSuccessHandler
func deployerSuccessHandler(w http.ResponseWriter, r *http.Request) {
valid := &Configuration{}
err := yaml.Unmarshal(validConfigurationTestCaseData, valid)
if err != nil {
status := fmt.Sprintf("cannot unmarshal test case data:%s", err)
http.Error(w, status, http.StatusInternalServerError)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
status := fmt.Sprintf("cannot read request body:%s", err)
http.Error(w, status, http.StatusInternalServerError)
return
}
result := &Configuration{}
if err := yaml.Unmarshal(body, result); err != nil {
status := fmt.Sprintf("cannot unmarshal request body:%s", err)
http.Error(w, status, http.StatusInternalServerError)
return
}
if !reflect.DeepEqual(valid, result) {
status := fmt.Sprintf("error in http handler:\nwant:%s\nhave:%s\n",
util.ToYAMLOrError(valid), util.ToYAMLOrError(result))
http.Error(w, status, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
开发者ID:vagababov,项目名称:deployment-manager,代码行数:33,代码来源:deployer_test.go
示例4: Unmarshal
// Unmarshal creates and returns an ExpandedTemplate from an ExpansionResponse.
func (er *ExpansionResponse) Unmarshal() (*ExpandedTemplate, error) {
template := &ExpandedTemplate{}
if err := yaml.Unmarshal([]byte(er.Config), &template.Config); err != nil {
return nil, fmt.Errorf("cannot unmarshal config (%s):\n%s", err, er.Config)
}
if err := yaml.Unmarshal([]byte(er.Layout), &template.Layout); err != nil {
return nil, fmt.Errorf("cannot unmarshal layout (%s):\n%s", err, er.Layout)
}
return template, nil
}
开发者ID:jiangyaoguo,项目名称:deployment-manager,代码行数:13,代码来源:expander.go
示例5: DecodeInto
// DecodeInto parses a YAML or JSON string and stores it in obj. Returns an error
// if data.Kind is set and doesn't match the type of obj. Obj should be a
// pointer to an api type.
// If obj's version doesn't match that in data, an attempt will be made to convert
// data into obj's version.
func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
if len(data) == 0 {
// This is valid YAML, but it's a bad idea not to return an error
// for an empty string-- that's almost certainly not what the caller
// was expecting.
return errors.New("empty input")
}
dataVersion, dataKind, err := s.DataVersionAndKind(data)
if err != nil {
return err
}
objVersion, objKind, err := s.ObjectVersionAndKind(obj)
if err != nil {
return err
}
if dataKind == "" {
// Assume objects with unset Kind fields are being unmarshalled into the
// correct type.
dataKind = objKind
}
if dataVersion == "" {
// Assume objects with unset Version fields are being unmarshalled into the
// correct type.
dataVersion = objVersion
}
if objVersion == dataVersion {
// Easy case!
err = yaml.Unmarshal(data, obj)
if err != nil {
return err
}
} else {
external, err := s.NewObject(dataVersion, dataKind)
if err != nil {
return err
}
// yaml is a superset of json, so we use it to decode here. That way,
// we understand both.
err = yaml.Unmarshal(data, external)
if err != nil {
return err
}
err = s.converter.Convert(external, obj, 0, s.generateConvertMeta(dataVersion, objVersion))
if err != nil {
return err
}
}
// Version and Kind should be blank in memory.
return s.SetVersionAndKind("", "", obj)
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:57,代码来源:decode.go
示例6: TestYAMLPrinterPrint
func TestYAMLPrinterPrint(t *testing.T) {
type testStruct struct {
Key string `json:"Key"`
Map map[string]int `json:"Map"`
StringList []string `json:"StringList"`
IntList []int `json:"IntList"`
}
testData := testStruct{
"testValue",
map[string]int{"TestSubkey": 1},
[]string{"a", "b", "c"},
[]int{1, 2, 3},
}
printer := &YAMLPrinter{}
buf := bytes.NewBuffer([]byte{})
err := printer.Print([]byte("invalidJSON"), buf)
if err == nil {
t.Error("Error: didn't fail on invalid JSON data")
}
jTestData, err := json.Marshal(&testData)
if err != nil {
t.Fatal("Unexpected error: couldn't marshal test data")
}
err = printer.Print(jTestData, buf)
if err != nil {
t.Fatal(err)
}
var poutput testStruct
err = yaml.Unmarshal(buf.Bytes(), &poutput)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(testData, poutput) {
t.Errorf("Test data and unmarshaled data are not equal: %#v vs %#v", poutput, testData)
}
obj := &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "foo"},
}
buf.Reset()
printer.PrintObj(obj, buf)
var objOut api.Pod
err = yaml.Unmarshal([]byte(buf.String()), &objOut)
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
if !reflect.DeepEqual(obj, &objOut) {
t.Errorf("Unexpected inequality: %#v vs %#v", obj, &objOut)
}
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:52,代码来源:resource_printer_test.go
示例7: tryDecodeSingle
func tryDecodeSingle(data []byte) (parsed bool, manifest v1beta1.ContainerManifest, pod api.Pod, err error) {
// TODO: should be api.Scheme.Decode
// This is awful. DecodeInto() expects to find an APIObject, which
// Manifest is not. We keep reading manifest for now for compat, but
// we will eventually change it to read Pod (at which point this all
// becomes nicer). Until then, we assert that the ContainerManifest
// structure on disk is always v1beta1. Read that, convert it to a
// "current" ContainerManifest (should be ~identical), then convert
// that to a Pod (which is a well-understood conversion). This
// avoids writing a v1beta1.ContainerManifest -> api.Pod
// conversion which would be identical to the api.ContainerManifest ->
// api.Pod conversion.
if err = yaml.Unmarshal(data, &manifest); err != nil {
return false, manifest, pod, err
}
newManifest := api.ContainerManifest{}
if err = api.Scheme.Convert(&manifest, &newManifest); err != nil {
return false, manifest, pod, err
}
if errs := validation.ValidateManifest(&newManifest); len(errs) > 0 {
err = fmt.Errorf("invalid manifest: %v", errs)
return false, manifest, pod, err
}
if err = api.Scheme.Convert(&newManifest, &pod); err != nil {
return true, manifest, pod, err
}
// Success.
return true, manifest, pod, nil
}
开发者ID:vrosnet,项目名称:kubernetes,代码行数:29,代码来源:http.go
示例8: TestStrategicMergePatch
func TestStrategicMergePatch(t *testing.T) {
tc := TestCases{}
err := yaml.Unmarshal(testCaseData, &tc)
if err != nil {
t.Errorf("can't unmarshal test cases: %v", err)
return
}
var e MergeItem
for _, c := range tc.StrategicMergePatchCases {
result, err := StrategicMergePatchData(toJSON(c.Original), toJSON(c.Patch), e)
if err != nil {
t.Errorf("error patching: %v:\noriginal:\n%s\npatch:\n%s",
err, toYAML(c.Original), toYAML(c.Patch))
}
// Sort the lists that have merged maps, since order is not significant.
result, err = sortMergeListsByName(result, e)
if err != nil {
t.Errorf("error sorting result object: %v", err)
}
cResult, err := sortMergeListsByName(toJSON(c.Result), e)
if err != nil {
t.Errorf("error sorting result object: %v", err)
}
if !reflect.DeepEqual(result, cResult) {
t.Errorf("patching failed: %s\noriginal:\n%s\npatch:\n%s\nexpected result:\n%s\ngot result:\n%s",
c.Description, toYAML(c.Original), toYAML(c.Patch), jsonToYAML(cResult), jsonToYAML(result))
}
}
}
开发者ID:SivagnanamCiena,项目名称:calico-kubernetes,代码行数:32,代码来源:patch_test.go
示例9: readSimpleService
func readSimpleService(filename string) SimpleService {
inData, err := ReadConfigData(filename)
checkErr(err)
simpleService := SimpleService{}
err = yaml.Unmarshal(inData, &simpleService)
checkErr(err)
if simpleService.Name == "" {
_, simpleService.Name = ParseDockerImage(simpleService.Image)
// TODO: encode/scrub the name
}
simpleService.Name = strings.ToLower(simpleService.Name)
// TODO: Validate the image name and extract exposed ports
// TODO: Do more validation
if !util.IsDNSLabel(simpleService.Name) {
checkErr(fmt.Errorf("name (%s) is not a valid DNS label", simpleService.Name))
}
if simpleService.Replicas == 0 {
simpleService.Replicas = 1
}
return simpleService
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:27,代码来源:simplegen.go
示例10: parseMessageBlock
// parseMessageBlock
func parseMessageBlock(data []byte) (*hapi.Metadata, *SumCollection, error) {
// This sucks.
parts := bytes.Split(data, []byte("\n...\n"))
if len(parts) < 2 {
return nil, nil, errors.New("message block must have at least two parts")
}
md := &hapi.Metadata{}
sc := &SumCollection{}
if err := yaml.Unmarshal(parts[0], md); err != nil {
return md, sc, err
}
err := yaml.Unmarshal(parts[1], sc)
return md, sc, err
}
开发者ID:kubernetes,项目名称:helm,代码行数:17,代码来源:sign.go
示例11: TestDurationMarshalJSONUnmarshalYAML
func TestDurationMarshalJSONUnmarshalYAML(t *testing.T) {
cases := []struct {
input Duration
}{
{Duration{}},
{Duration{5 * time.Second}},
{Duration{2 * time.Minute}},
{Duration{time.Hour + 3*time.Millisecond}},
}
for i, c := range cases {
input := DurationHolder{c.input}
jsonMarshalled, err := json.Marshal(&input)
if err != nil {
t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
}
var result DurationHolder
if err := yaml.Unmarshal(jsonMarshalled, &result); err != nil {
t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
}
if input.D != result.D {
t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
}
}
}
开发者ID:alex-mohr,项目名称:kubernetes,代码行数:27,代码来源:duration_test.go
示例12: MergeAttributesFilesForMap
func MergeAttributesFilesForMap(omap map[string]interface{}, files []string) map[string]interface{} {
newMap := make(map[string]interface{})
newMap["default"] = omap
// loop over attributes files
// merge override files to default files
for _, file := range files {
var data interface{}
yml, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
// yaml to data
err = yaml.Unmarshal(yml, &data)
if err != nil {
panic(err)
}
data, err = transform(data)
if err != nil {
panic(err)
}
// data to map
json := data.(map[string]interface{})
omap = mergemap.Merge(newMap, json)
}
return ProcessOverride(newMap)
}
开发者ID:HimanshPal,项目名称:cnt,代码行数:28,代码来源:merger.go
示例13: toCommand
// helper function to encode the build step to
// a json string. Primarily used for plugins, which
// expect a json encoded string in stdin or arg[1].
func toCommand(s *State, n *parser.DockerNode) []string {
p := payload{
Workspace: s.Workspace,
Repo: s.Repo,
Build: s.Build,
Job: s.Job,
Vargs: n.Vargs,
}
y, err := yaml.Marshal(n.Vargs)
if err != nil {
log.Debug(err)
}
p.Vargs = map[string]interface{}{}
err = yamljson.Unmarshal(y, &p.Vargs)
if err != nil {
log.Debug(err)
}
p.System = &plugin.System{
Version: s.System.Version,
Link: s.System.Link,
}
b, _ := json.Marshal(p)
return []string{"--", string(b)}
}
开发者ID:CiscoCloud,项目名称:drone-exec,代码行数:30,代码来源:utils.go
示例14: parseCredentials
func parseCredentials(bytes []byte) ([]NamedRegistryCredential, error) {
r := []NamedRegistryCredential{}
if err := yaml.Unmarshal(bytes, &r); err != nil {
return []NamedRegistryCredential{}, fmt.Errorf("cannot unmarshal credentials file (%#v)", err)
}
return r, nil
}
开发者ID:jtblin,项目名称:deployment-manager,代码行数:7,代码来源:filebased_credential_provider.go
示例15: 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
}
开发者ID:bmelville,项目名称:deployment-manager,代码行数:25,代码来源:deployer.go
示例16: extractFromFile
func extractFromFile(filename string) (api.BoundPod, error) {
var pod api.BoundPod
glog.V(3).Infof("Reading config file %q", filename)
file, err := os.Open(filename)
if err != nil {
return pod, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return pod, err
}
manifest := &api.ContainerManifest{}
// TODO: use api.Scheme.DecodeInto
if err := yaml.Unmarshal(data, manifest); err != nil {
return pod, fmt.Errorf("can't unmarshal file %q: %v", filename, err)
}
if err := api.Scheme.Convert(manifest, &pod); err != nil {
return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err)
}
hostname, err := os.Hostname() //TODO: kubelet name would be better
if err != nil {
return pod, err
}
if len(pod.UID) == 0 {
hasher := md5.New()
fmt.Fprintf(hasher, "host:%s", hostname)
fmt.Fprintf(hasher, "file:%s", filename)
util.DeepHashObject(hasher, pod)
pod.UID = hex.EncodeToString(hasher.Sum(nil)[0:])
glog.V(5).Infof("Generated UID %q for pod %q from file %s", pod.UID, pod.Name, filename)
}
if len(pod.Namespace) == 0 {
hasher := adler32.New()
fmt.Fprint(hasher, filename)
// TODO: file-<sum>.hostname would be better, if DNS subdomains
// are allowed for namespace (some places only allow DNS
// labels).
pod.Namespace = fmt.Sprintf("file-%08x-%s", hasher.Sum32(), hostname)
glog.V(5).Infof("Generated namespace %q for pod %q from file %s", pod.Namespace, pod.Name, filename)
}
// TODO(dchen1107): BoundPod is not type of runtime.Object. Once we allow kubelet talks
// about Pod directly, we can use SelfLinker defined in package: latest
// Currently just simply follow the same format in resthandler.go
pod.ObjectMeta.SelfLink = fmt.Sprintf("/api/v1beta2/pods/%s?namespace=%s",
pod.Name, pod.Namespace)
if glog.V(4) {
glog.Infof("Got pod from file %q: %#v", filename, pod)
} else {
glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID)
}
return pod, nil
}
开发者ID:nhr,项目名称:kubernetes,代码行数:60,代码来源:file.go
示例17: Unmarshal
// Unmarshal creates and returns an ExpansionResult from an ExpansionResponse.
func (eResponse *ExpansionResponse) Unmarshal() (*ExpansionResult, error) {
var config map[string]interface{}
if err := yaml.Unmarshal([]byte(eResponse.Config), &config); err != nil {
return nil, fmt.Errorf("cannot unmarshal config (%s):\n%s", err, eResponse.Config)
}
var layout map[string]interface{}
if err := yaml.Unmarshal([]byte(eResponse.Layout), &layout); err != nil {
return nil, fmt.Errorf("cannot unmarshal layout (%s):\n%s", err, eResponse.Layout)
}
return &ExpansionResult{
Config: config,
Layout: layout,
}, nil
}
开发者ID:RamessesYin,项目名称:deployment-manager,代码行数:17,代码来源:expander.go
示例18: newEtcdController
func newEtcdController(kubeBoot *KubeBoot, v *Volume, spec *EtcdClusterSpec) (*EtcdController, error) {
k := &EtcdController{
kubeBoot: kubeBoot,
}
modelTemplatePath := path.Join(kubeBoot.ModelDir, spec.ClusterKey+".config")
modelTemplate, err := ioutil.ReadFile(modelTemplatePath)
if err != nil {
return nil, fmt.Errorf("error reading model template %q: %v", modelTemplatePath, err)
}
cluster := &EtcdCluster{}
cluster.Spec = spec
cluster.VolumeMountPath = v.Mountpoint
model, err := ExecuteTemplate("model-etcd-"+spec.ClusterKey, string(modelTemplate), cluster)
if err != nil {
return nil, fmt.Errorf("error executing etcd model template %q: %v", modelTemplatePath, err)
}
err = yaml.Unmarshal([]byte(model), cluster)
if err != nil {
return nil, fmt.Errorf("error parsing etcd model template %q: %v", modelTemplatePath, err)
}
k.cluster = cluster
return k, nil
}
开发者ID:crohling,项目名称:kops,代码行数:28,代码来源:etcd_cluster.go
示例19: TestDurationUnmarshalYAML
func TestDurationUnmarshalYAML(t *testing.T) {
cases := []struct {
input string
result Duration
}{
{"d: 0s\n", Duration{}},
{"d: 5s\n", Duration{5 * time.Second}},
{"d: 2m0s\n", Duration{2 * time.Minute}},
{"d: 1h0m0.003s\n", Duration{time.Hour + 3*time.Millisecond}},
// Units with zero values can optionally be dropped
{"d: 2m\n", Duration{2 * time.Minute}},
{"d: 1h0.003s\n", Duration{time.Hour + 3*time.Millisecond}},
}
for _, c := range cases {
var result DurationHolder
if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
t.Errorf("Failed to unmarshal input %q: %v", c.input, err)
}
if result.D != c.result {
t.Errorf("Failed to unmarshal input %q: expected %q, got %q", c.input, c.result, result)
}
}
}
开发者ID:alex-mohr,项目名称:kubernetes,代码行数:25,代码来源:duration_test.go
示例20: DataToObjects
// DataToObjects converts the raw JSON data into API objects
func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (result []runtime.Object, errors []error) {
configObj := []runtime.RawExtension{}
if err := yaml.Unmarshal(data, &configObj); err != nil {
errors = append(errors, fmt.Errorf("config unmarshal: %v", err))
return result, errors
}
for i, in := range configObj {
version, kind, err := t.DataVersionAndKind(in.RawJSON)
if err != nil {
errors = append(errors, fmt.Errorf("item[%d] kind: %v", i, err))
continue
}
mapping, err := m.RESTMapping(kind, version)
if err != nil {
errors = append(errors, fmt.Errorf("item[%d] mapping: %v", i, err))
continue
}
obj, err := mapping.Codec.Decode(in.RawJSON)
if err != nil {
errors = append(errors, fmt.Errorf("item[%d] decode: %v", i, err))
continue
}
result = append(result, obj)
}
return
}
开发者ID:nhr,项目名称:kubernetes,代码行数:31,代码来源:createall.go
注:本文中的github.com/ghodss/yaml.Unmarshal函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论