当前位置: 首页>>代码示例>>Golang>>正文


Golang yaml.Unmarshal函数代码示例

本文整理汇总了Golang中github.com/go-yaml/yaml.Unmarshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Unmarshal函数的具体用法?Golang Unmarshal怎么用?Golang Unmarshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Unmarshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ReadConfig

// ReadConfig parses the configuration file provided and returns
// ReadConfig reads config from file to structure
func ReadConfig(fname string) (Config, error) {
	// Created new...
	config := &Config{}
	yamlConfig := yamlConfig{}
	if fname != "" {
		data, err := ioutil.ReadFile(fname)
		if err != nil {
			return *config, err
		}
		err = yaml.Unmarshal([]byte(data), &yamlConfig)
		if err != nil {
			return *config, err
		}
		serviceConfigs := yamlConfig.Services
		config.Services = make(map[string]ServiceConfig)
		// Now convert this to map for easier reading...
		for i := range serviceConfigs {
			c := serviceConfigs[i]
			api := Api{Host: c.Api.Host, Port: c.Api.Port}

			cleanedConfig := cleanupMap(c.Config)
			config.Services[c.Service] = ServiceConfig{CommonConfig{&api}, cleanedConfig}

		}
		log.Println("Read configuration from", fname)
		return *config, nil
	} else {
		return *config, errors.New("Empty filename.")
	}
}
开发者ID:yangzhares,项目名称:core,代码行数:32,代码来源:config.go

示例2: TestYamlLink

func TestYamlLink(t *testing.T) {
	assertions := map[string]string{
		"platform.statsd":          "platform.statsd:statsd",
		"platform.statsd:metrics":  "platform.statsd:metrics",
		"/platform.statsd":         "platform.statsd:statsd",
		"/platform.statsd:metrics": "platform.statsd:metrics",
		"statsd":                   "statsd:statsd",
		"statsd:metrics":           "statsd:metrics",
		".statsd":                  "statsd:statsd",
		".statsd:metrics":          "statsd:metrics",
		"":                         "\"\"",
	}

	for inYaml, outYaml := range assertions {
		v := Link{}
		if err := yaml.Unmarshal([]byte(inYaml), &v); err != nil {
			t.Fatal(err)
		}
		data, err := yaml.Marshal(v)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, outYaml, strings.TrimSpace(string(data)))
	}
}
开发者ID:pra85,项目名称:rocker-compose,代码行数:25,代码来源:yaml_test.go

示例3: newBot

func newBot() (b MMJira) {

	b = MMJira{l: zap.NewJSON(zap.DebugLevel), reg: metrics.NewRegistry()}
	data, err := ioutil.ReadFile("config.yaml")
	if err != nil {
		b.l.Panic("not able to read the file", zap.Error(err))
	}
	var config InstanceConfig
	if err = yaml.Unmarshal(data, &config); err != nil {
		b.l.Panic("not able to marshal the file", zap.Error(err))
	}
	b.c = &config
	if !b.c.Debug {
		b.l.SetLevel(zap.ErrorLevel)
	}
	mmpost, err := mmcontroller.NewController(b.c.MMicon, b.c.MMuser, b.c.Hooks, b.c.Debug, metrics.NewPrefixedChildRegistry(b.reg, "mmc."))
	if err != nil {
		panic(err)
	}

	b.m = mmpost
	b.l.Debug("outputting config", zap.Object("config", b.c))
	b.r = mux.NewRouter()
	b.r.HandleFunc("/", b.homeHandler)
	b.r.HandleFunc("/hooks/", b.getHandler).Methods("GET")
	b.r.HandleFunc("/hooks/{hookid}", b.postHandler).Methods("POST")
	b.r.Handle("/metrics", exp.ExpHandler(b.reg))
	b.r.HandleFunc("/config/", b.configGetHandler).Methods("GET")

	return b

}
开发者ID:tixu,项目名称:mmjira,代码行数:32,代码来源:main.go

示例4: TestPathUnmarshalYAML

func TestPathUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		device Path
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"/path"`},
			out: out{device: Path("/path")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{device: Path("bad"), err: ErrPathRelative},
		},
	}

	for i, test := range tests {
		var device Path
		err := yaml.Unmarshal([]byte(test.in.data), &device)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.device, device) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.device, device)
		}
	}
}
开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:34,代码来源:path_test.go

示例5: VarsFromFile

// VarsFromFile reads variables from either JSON or YAML file
func VarsFromFile(filename string) (vars Vars, err error) {
	log.Debugf("Load vars from file %s", filename)

	if filename, err = resolveFileName(filename); err != nil {
		return nil, err
	}

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	vars = Vars{}

	switch filepath.Ext(filename) {
	case ".yaml", ".yml", ".":
		if err := yaml.Unmarshal(data, &vars); err != nil {
			return nil, err
		}
	case ".json":
		if err := json.Unmarshal(data, &vars); err != nil {
			return nil, err
		}
	}

	return vars, nil
}
开发者ID:romank87,项目名称:rocker,代码行数:28,代码来源:vars.go

示例6: TestMkfsOptionsUnmarshalYAML

func TestMkfsOptionsUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		options MkfsOptions
		err     error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `["--label=ROOT"]`},
			out: out{options: MkfsOptions([]string{"--label=ROOT"})},
		},
	}

	for i, test := range tests {
		var options MkfsOptions
		err := yaml.Unmarshal([]byte(test.in.data), &options)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.options, options) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.options, options)
		}
	}
}
开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:30,代码来源:filesystem_test.go

示例7: TestHashUnmarshalYAML

func TestHashUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		hash Hash
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef`},
			out: out{hash: Hash{Function: "sha512", Sum: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}},
		},
		{
			in:  in{data: `xor01234567`},
			out: out{err: ErrHashMalformed},
		},
	}

	for i, test := range tests {
		var hash Hash
		err := yaml.Unmarshal([]byte(test.in.data), &hash)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.hash, hash) {
			t.Errorf("#%d: bad hash: want %+v, got %+v", i, test.out.hash, hash)
		}
	}
}
开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:34,代码来源:hash_test.go

示例8: TestFilesystemUnmarshalYAML

func TestFilesystemUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		filesystem Filesystem
		err        error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: "mount:\n  device: /foo\n  format: ext4"},
			out: out{filesystem: Filesystem{Mount: &FilesystemMount{Device: "/foo", Format: "ext4"}}},
		},
		{
			in:  in{data: "mount:\n  format: ext4"},
			out: out{err: ErrPathRelative},
		},
	}

	for i, test := range tests {
		var filesystem Filesystem
		err := yaml.Unmarshal([]byte(test.in.data), &filesystem)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.filesystem, filesystem) {
			t.Errorf("#%d: bad filesystem: want %#v, got %#v", i, test.out.filesystem, filesystem)
		}
	}
}
开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:34,代码来源:filesystem_test.go

示例9: TestFilesystemFormatUnmarshalYAML

func TestFilesystemFormatUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		format FilesystemFormat
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"ext4"`},
			out: out{format: FilesystemFormat("ext4")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{format: FilesystemFormat("bad"), err: ErrFilesystemInvalidFormat},
		},
	}

	for i, test := range tests {
		var format FilesystemFormat
		err := yaml.Unmarshal([]byte(test.in.data), &format)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.format, format) {
			t.Errorf("#%d: bad format: want %#v, got %#v", i, test.out.format, format)
		}
	}
}
开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:34,代码来源:filesystem_test.go

示例10: ReadConfigBytes

// ReadConfigBytes reads the supplied configuration byte[] and returns the
// corresponding map or an error.
func ReadConfigBytes(configSetting []byte) (*map[interface{}]interface{}, error) {
	configData := make(map[interface{}]interface{})
	err := yaml.Unmarshal(configSetting, &configData)
	if err != nil {
		return nil, err
	}
	return &configData, nil
}
开发者ID:bluele,项目名称:walter,代码行数:10,代码来源:config.go

示例11: NewMigrationFromGull

func NewMigrationFromGull(name string, source string) (*Migration, error) {
	migration := newMigration(name)

	sourceBytes, err := ingestMigrationTemplate(source)
	if err != nil {
		return nil, err
	}

	err = yaml.Unmarshal(sourceBytes, &migration.Content)

	return migration, err
}
开发者ID:C2FO,项目名称:gull,代码行数:12,代码来源:migration.go

示例12: Load

// Load unmarshals YAML data from the filesystem
func Load(path string, result interface{}) error {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	if err := yaml.Unmarshal(data, result); err != nil {
		return err
	}

	return nil
}
开发者ID:vbatoufflet,项目名称:goverview,代码行数:13,代码来源:yaml.go

示例13: parseConfFile

func (c *Config) parseConfFile() error {
	content, err := ioutil.ReadFile(c.confFile)
	if err != nil {
		return err
	}
	c.confParsed = make(map[interface{}]interface{})
	err = yaml.Unmarshal(content, &(c.confParsed))
	if err != nil {
		return err
	}
	return nil
}
开发者ID:dzch,项目名称:binstore,代码行数:12,代码来源:config.go

示例14: ConfigFromFile

func ConfigFromFile(configPath string) (*Config, error) {
	configBytes, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}

	config := Config{}
	if err := goyaml.Unmarshal(configBytes, &config); err != nil {
		return nil, err
	}

	return &config, nil
}
开发者ID:nagyistge,项目名称:dea_ng,代码行数:13,代码来源:config.go

示例15: newConfig

func newConfig(in io.Reader) (*config, error) {
	buf, err := ioutil.ReadAll(in)
	if err != nil {
		return nil, err
	}

	config := &config{}
	if err := yaml.Unmarshal(buf, config); err != nil {
		return nil, err
	}

	return config, nil
}
开发者ID:FPurchess,项目名称:blank,代码行数:13,代码来源:config.go


注:本文中的github.com/go-yaml/yaml.Unmarshal函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。