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


Golang goyaml.Unmarshal函数代码示例

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


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

示例1: Loop

func (r *RedisConnector) Loop(parsed chan mgollective.Message) {
	for msg := range r.subs.Messages {
		// ruby symbols/YAML encoding is special
		// Pretend like it was just a string with a colon
		silly_ruby, _ := regexp.Compile("!ruby/sym ")
		wire := silly_ruby.ReplaceAll(msg.Elem, []byte(":"))

		var wrapper RedisMessageWrapper
		if err := goyaml.Unmarshal(wire, &wrapper); err != nil {
			r.app.Debug("YAML Unmarshal wrapper", err)
			r.app.Info("Recieved undecodable message, skipping it")
			continue
		}
		r.app.Tracef("unpackged wrapper %+v", wrapper)

		var body mgollective.MessageBody
		if err := goyaml.Unmarshal([]byte(wrapper.Body), &body); err != nil {
			r.app.Debug("YAML Unmarshal body", err)
			continue
		}

		message := mgollective.Message{
			Topic:    msg.Channel,
			Reply_to: wrapper.Headers["reply-to"],
			Body:     body,
		}
		parsed <- message
	}
}
开发者ID:richardc,项目名称:mgollective,代码行数:29,代码来源:connector_redis.go

示例2: TestGetConfig

func (s *GetSuite) TestGetConfig(c *gc.C) {
	sch := s.AddTestingCharm(c, "dummy")
	svc := s.AddTestingService(c, "dummy-service", sch)
	err := svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"})
	c.Assert(err, gc.IsNil)
	for _, t := range getTests {
		ctx := coretesting.Context(c)
		code := cmd.Main(&GetCommand{}, ctx, []string{t.service})
		c.Check(code, gc.Equals, 0)
		c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
		// round trip via goyaml to avoid being sucked into a quagmire of
		// map[interface{}]interface{} vs map[string]interface{}. This is
		// also required if we add json support to this command.
		buf, err := goyaml.Marshal(t.expected)
		c.Assert(err, gc.IsNil)
		expected := make(map[string]interface{})
		err = goyaml.Unmarshal(buf, &expected)
		c.Assert(err, gc.IsNil)

		actual := make(map[string]interface{})
		err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
		c.Assert(err, gc.IsNil)
		c.Assert(actual, gc.DeepEquals, expected)
	}
}
开发者ID:jameinel,项目名称:core,代码行数:25,代码来源:get_test.go

示例3: ReadHostsYAML

// A function to read an hosts file in the YAML format and returns
// a dictionary in the same format as the structured file.
func ReadHostsYAML(
	filename string,
) *Hosts {

	// Start by reading the whole file in byte
	data, _ := ioutil.ReadFile(filename)

	// Create the variable handling the type of the user file
	t := &Hosts{}

	// Now read in the YAML file the structure of the file into
	// the structured dictionary
	err := goyaml.Unmarshal(
		data,
		t,
	)

	// Check error when reading the file
	if err != nil {
		formatter.ColoredPrintln(
			formatter.Red,
			false,
			"The file "+filename+" can't be read for accessing"+
				"the YAML structure!\n"+
				"Reason is: "+err.Error(),
		)
		return nil
	}

	// return the structured file and data
	return t

}
开发者ID:ElricleNecro,项目名称:TOD,代码行数:35,代码来源:hosts.go

示例4: TestUnmarshal

func (s *S) TestUnmarshal(c *C) {
	for i, item := range unmarshalTests {
		t := reflect.ValueOf(item.value).Type()
		var value interface{}
		switch t.Kind() {
		case reflect.Map:
			value = reflect.MakeMap(t).Interface()
		case reflect.String:
			t := reflect.ValueOf(item.value).Type()
			v := reflect.New(t)
			value = v.Interface()
		default:
			pt := reflect.ValueOf(item.value).Type()
			pv := reflect.New(pt.Elem())
			value = pv.Interface()
		}
		err := goyaml.Unmarshal([]byte(item.data), value)
		c.Assert(err, IsNil, Commentf("Item #%d", i))
		if t.Kind() == reflect.String {
			c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i))
		} else {
			c.Assert(value, DeepEquals, item.value, Commentf("Item #%d", i))
		}
	}
}
开发者ID:9cc9,项目名称:dea_ng,代码行数:25,代码来源:decode_test.go

示例5: LoadConfiguration

func LoadConfiguration(configPath *string) {
	if !Exists(*configPath) {
		return
	}

	m := make(map[interface{}]interface{})
	data, err := ioutil.ReadFile(*configPath)
	if err != nil {
		panic(fmt.Sprintf("Error reading file %s : %v", configPath, err))
	}

	goyaml.Unmarshal([]byte(data), &m)

	if val, ok := m["sourceDirectory"]; ok {
		Configuration.SourcePath = val.(string)
	}

	if val, ok := m["destinationDirectory"]; ok {
		Configuration.DestinationPath = val.(string)
	}

	if val, ok := m["processFilesWithExtension"]; ok {
		vals := val.([]interface{})
		exts := make([]string, len(vals))
		for idx, val := range vals {
			exts[idx] = val.(string)
		}
		Configuration.ProcessFileExtensions = exts
	}
}
开发者ID:mehulsbhatt,项目名称:sandbox-1,代码行数:30,代码来源:configuration.go

示例6: Set

// Set decodes the base64 value into yaml then expands that into a map.
func (v *yamlBase64Value) Set(value string) error {
	decoded, err := base64.StdEncoding.DecodeString(value)
	if err != nil {
		return err
	}
	return goyaml.Unmarshal(decoded, v)
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:8,代码来源:bootstrap.go

示例7: CollectStatus

func (p *JujuProvisioner) CollectStatus() ([]provision.Unit, error) {
	output, err := execWithTimeout(30e9, "juju", "status")
	if err != nil {
		return nil, &provision.Error{Reason: string(output), Err: err}
	}
	var out jujuOutput
	err = goyaml.Unmarshal(output, &out)
	if err != nil {
		return nil, &provision.Error{Reason: `"juju status" returned invalid data`, Err: err}
	}
	var units []provision.Unit
	for name, service := range out.Services {
		for unitName, u := range service.Units {
			machine := out.Machines[u.Machine]
			unit := provision.Unit{
				Name:       unitName,
				AppName:    name,
				Machine:    u.Machine,
				InstanceId: machine.InstanceId,
				Ip:         machine.IpAddress,
			}
			typeRegexp := regexp.MustCompile(`^(local:)?(\w+)/(\w+)-\d+$`)
			matchs := typeRegexp.FindStringSubmatch(service.Charm)
			if len(matchs) > 3 {
				unit.Type = matchs[3]
			}
			unit.Status = unitStatus(machine.InstanceState, u.AgentState, machine.AgentState)
			units = append(units, unit)
		}
	}
	return units, nil
}
开发者ID:elimisteve,项目名称:tsuru,代码行数:32,代码来源:provisioner.go

示例8: check

func (t *cloudinitTest) check(c *C) {
	ci, err := cloudinit.New(&t.cfg)
	c.Assert(err, IsNil)
	c.Check(ci, NotNil)
	// render the cloudinit config to bytes, and then
	// back to a map so we can introspect it without
	// worrying about internal details of the cloudinit
	// package.
	data, err := ci.Render()
	c.Assert(err, IsNil)

	x := make(map[interface{}]interface{})
	err = goyaml.Unmarshal(data, &x)
	c.Assert(err, IsNil)

	c.Check(x["apt_upgrade"], Equals, true)
	c.Check(x["apt_update"], Equals, true)

	scripts := getScripts(x)
	scriptDiff(c, scripts, t.expectScripts)
	if t.cfg.Config != nil {
		checkEnvConfig(c, t.cfg.Config, x, scripts)
	}
	checkPackage(c, x, "git", true)
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:25,代码来源:cloudinit_test.go

示例9: ReadEnvironsBytes

// ReadEnvironsBytes parses the contents of an environments.yaml file
// and returns its representation. An environment with an unknown type
// will only generate an error when New is called for that environment.
// Attributes for environments with known types are checked.
func ReadEnvironsBytes(data []byte) (*Environs, error) {
	var raw struct {
		Default      string
		Environments map[string]map[string]interface{}
	}
	err := goyaml.Unmarshal(data, &raw)
	if err != nil {
		return nil, err
	}

	if raw.Default != "" && raw.Environments[raw.Default] == nil {
		return nil, fmt.Errorf("default environment %q does not exist", raw.Default)
	}
	if raw.Default == "" {
		// If there's a single environment, then we get the default
		// automatically.
		if len(raw.Environments) == 1 {
			for name := range raw.Environments {
				raw.Default = name
				break
			}
		}
	}
	for name, attrs := range raw.Environments {
		// store the name of the this environment in the config itself
		// so that providers can see it.
		attrs["name"] = name
	}
	return &Environs{raw.Default, raw.Environments}, nil
}
开发者ID:klyachin,项目名称:juju,代码行数:34,代码来源:config.go

示例10: main

func main() {
	var (
		filename string
	)
	verbose = flag.Bool("v", false, "verbose mode, displays a line for every instance")
	flag.Parse()
	if flag.NArg() == 1 {
		filename = flag.Arg(0)
	} else {
		panic("Usage: test-instances filename.yaml\n")
	}
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	data := make([]byte, 1000000)
	count, err := file.Read(data)
	if err != nil {
		panic(err)
	}
	list := endpointsArray{}
	err = goyaml.Unmarshal(data[:count], &list)
	if err != nil {
		panic(err)
	}
	toReporter := make(chan instanceTest)
	fromReporter := make(chan string)
	go reporter(toReporter, fromReporter, len(list))

	for i := 0; i < len(list); i++ {
		go checkOne(toReporter, list[i].Endpoint)
	}
	<-fromReporter
}
开发者ID:bortzmeyer,项目名称:dns-lg,代码行数:34,代码来源:test-instances.go

示例11: SaveAuthToken

func SaveAuthToken(configPath, authtoken string) (err error) {
	// empty configuration by default for the case that we can't read it
	c := new(Configuration)

	// read the configuration
	oldConfigBytes, err := ioutil.ReadFile(configPath)
	if err == nil {
		// unmarshal if we successfully read the configuration file
		if err = goyaml.Unmarshal(oldConfigBytes, c); err != nil {
			return
		}
	}

	// no need to save, the authtoken is already the correct value
	if c.AuthToken == authtoken {
		return
	}

	// update auth token
	c.AuthToken = authtoken

	// rewrite configuration
	newConfigBytes, err := goyaml.Marshal(c)
	if err != nil {
		return
	}

	err = ioutil.WriteFile(configPath, newConfigBytes, 0600)
	return
}
开发者ID:BillSun,项目名称:ngrok,代码行数:30,代码来源:config.go

示例12: parseConfigFile

func parseConfigFile(n string) (*proxyHandler, error) {
	file, err := os.Open(n)

	if err != nil {
		return nil, err
	}

	var h *proxyHandler
	data, err := ioutil.ReadAll(file)

	if err != nil {
		return nil, err
	}

	err = goyaml.Unmarshal(data, &h)
	if err != nil {
		return nil, err
	}

	if h.Watermark != "" {
		file, err = os.Open(h.Watermark)
		h.watermarkImage, _, err = image.Decode(file)
		if err != nil {
			return nil, err
		}
	}

	h.backgroundColor = color.RGBA{80, 80, 80, 1}
	return h, nil
}
开发者ID:42floors,项目名称:waitress,代码行数:30,代码来源:server.go

示例13: extractPageConfig

/*
 * Extracts first HTML commend as map. It expects it as a valid YAML map.
 */
func (gen *Generator) extractPageConfig(doc *html.HtmlDocument) (config map[interface{}]interface{}, err error) {
	result, _ := doc.Search("//comment()")
	if len(result) > 0 {
		err = yaml.Unmarshal([]byte(result[0].Content()), &config)
	}
	return
}
开发者ID:kublaj,项目名称:zas,代码行数:10,代码来源:generate.go

示例14: HandleYamlMetaData

func HandleYamlMetaData(datum []byte) (interface{}, error) {
	m := map[string]interface{}{}
	if err := goyaml.Unmarshal(datum, &m); err != nil {
		return m, err
	}
	return m, nil
}
开发者ID:GeertJohan,项目名称:hugo,代码行数:7,代码来源:frontmatter.go

示例15: parseConfigFile

func parseConfigFile(fn string) {
	file, err := os.Open(fn)
	if err != nil {
		log.Error("failed to open %s: %s", fn, err)
		return
	}

	b, err := ioutil.ReadAll(file)
	if err != nil {
		log.Error("failed reading from %s: %s", fn, err)
		return
	}

	newstanzas := make(map[string]*sngConfig)
	err = yaml.Unmarshal(b, &newstanzas)
	if err != nil {
		log.Error("file is invalid %s: %s", fn, err)
	}

	// Validate configuration stanzas. This ensures that something is not
	// terribly broken with a config.
	for k, v := range newstanzas {
		v.Version = cfgVersion // Force to current version.
		v.Name = k
		v.Running = false
		mergeStanza(v)
	}
	synchronizeStanzas()
}
开发者ID:zorkian,项目名称:singularity,代码行数:29,代码来源:config.go


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