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


Golang toml.Unmarshal函数代码示例

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


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

示例1: TestConfig

func TestConfig(t *testing.T) {
	var configData = make(map[string]interface{})
	err := toml.Unmarshal([]byte(testTxt), &configData)
	if err != nil {
		t.Fatal(err)
	}

	cfg := NewMapConfig(configData)

	for k, v := range stringTable {
		if x := cfg.GetString(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

	for k, v := range intTable {
		if x := cfg.GetInt(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

	for k, v := range boolTable {
		if x := cfg.GetBool(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

}
开发者ID:tendermint,项目名称:go-config,代码行数:28,代码来源:config_test.go

示例2: UnmarshalData

// UnmarshalData unmarshal YAML/JSON/TOML serialized data.
func UnmarshalData(cont []byte, f DataFmt) (map[string]interface{}, error) {
	v := make(map[string]interface{})

	switch f {
	case YAML:
		log.Info("Unmarshaling YAML data")
		err := yaml.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	case TOML:
		log.Info("Unmarshaling TOML data")
		err := toml.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	case JSON:
		log.Info("Unmarshaling JSON data")
		err := json.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	default:
		log.Error("Unsupported data format")
		return nil, errors.New("Unsupported data format")
	}

	return v, nil
}
开发者ID:mickep76,项目名称:go-sampleapp,代码行数:30,代码来源:input.go

示例3: MustParseConfig

func MustParseConfig(configFile string) {
	if fileInfo, err := os.Stat(configFile); err != nil {
		if os.IsNotExist(err) {
			log.Panicf("configuration file %v does not exist.", configFile)
		} else {
			log.Panicf("configuration file %v can not be stated. %v", err)
		}
	} else {
		if fileInfo.IsDir() {
			log.Panicf("%v is a directory name", configFile)
		}
	}

	content, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Panicf("read configuration file error. %v", err)
	}
	content = bytes.TrimSpace(content)

	err = toml.Unmarshal(content, &Conf)
	if err != nil {
		log.Panicf("unmarshal toml object error. %v", err)
	}

	// short url black list
	Conf.Common.BlackShortURLsMap = make(map[string]bool)
	for _, blackShortURL := range Conf.Common.BlackShortURLs {
		Conf.Common.BlackShortURLsMap[blackShortURL] = true
	}

	// base string
	Conf.Common.BaseStringLength = uint64(len(Conf.Common.BaseString))
}
开发者ID:xiaogao0371,项目名称:dockerfile,代码行数:33,代码来源:conf.go

示例4: NewPostsFrontMatter

// NewPostsFrontMatter parse post meta file to create post data
func NewPostsFrontMatter(file string, t FormatType) (map[string]*Post, error) {
	metas := make(map[string]*Post)

	if t == FormatTOML {
		data, err := ioutil.ReadFile(file)
		if err != nil {
			return nil, err
		}
		if err = toml.Unmarshal(data, &metas); err != nil {
			return nil, err
		}
	}

	if t == FormatINI {
		iniObj, err := ini.Load(file)
		if err != nil {
			return nil, err
		}
		for _, s := range iniObj.SectionStrings() {
			s2 := strings.Trim(s, `"`)
			if s2 == "DEFAULT" {
				continue
			}
			post := new(Post)
			if err = newPostFromIniSection(iniObj.Section(filepath.ToSlash(s)), post); err != nil {
				return nil, err
			}
			metas[s2] = post
		}
	}
	return metas, nil
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:33,代码来源:front_matter.go

示例5: LoadCfg

func LoadCfg() (*Cfg, error) {
	flag.Parse()

	bytes, err := ioutil.ReadFile(*configPath)
	if err != nil {
		return nil, err
	}

	conf := Cfg{}
	if err := toml.Unmarshal(bytes, &conf); err != nil {
		return nil, err
	}

	if *staticsPath != "" {
		conf.StaticsPath = *staticsPath
	}

	if *host != "" {
		conf.Host = *host
	}

	if *httpDrainInterval != "" {
		conf.HttpDrainInterval = *httpDrainInterval
	}

	if *boltDDPath != "" {
		conf.BoltDDPath = *boltDDPath
	}

	conf.BatchSize = *batchSize

	return &conf, nil
}
开发者ID:mateuszdyminski,项目名称:logag,代码行数:33,代码来源:cfg.go

示例6: NewPageOfMarkdown

// NewPageOfMarkdown create new page from markdown file
func NewPageOfMarkdown(file, slug string, page *Page) (*Page, error) {
	// page-node need not read file
	if page != nil && page.Node == true {
		return page, nil
	}
	fileBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	if len(fileBytes) < 3 {
		return nil, fmt.Errorf("page content is too less")
	}
	if page == nil {
		dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
		if len(dataSlice) != 3 {
			return nil, fmt.Errorf("page need front-matter block and markdown block")
		}

		idx := getFirstBreakByte(dataSlice[1])
		if idx == 0 {
			return nil, fmt.Errorf("page need front-matter block and markdown block")
		}

		formatType := detectFormat(string(dataSlice[1][:idx]))
		if formatType == 0 {
			return nil, fmt.Errorf("page front-matter block is unrecognized")
		}

		page = new(Page)
		if formatType == FormatTOML {
			if err = toml.Unmarshal(dataSlice[1][idx:], page); err != nil {
				return nil, err
			}
		}
		if formatType == FormatINI {
			iniObj, err := ini.Load(dataSlice[1][idx:])
			if err != nil {
				return nil, err
			}
			if err = newPageFromIniObject(iniObj, page, "DEFAULT", "meta"); err != nil {
				return nil, err
			}
		}
		if page.Node == false {
			page.Bytes = bytes.Trim(dataSlice[2], "\n")
		}
	} else {
		page.Bytes = bytes.Trim(fileBytes, "\n")
	}
	page.fileURL = file
	if page.Slug == "" {
		page.Slug = slug
	}
	if page.Date == "" && page.Node == false { // page-node need not time
		t, _ := com.FileMTime(file)
		page.dateTime = time.Unix(t, 0)
	}
	return page, page.normalize()
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:60,代码来源:page.go

示例7: loadConfig

func loadConfig() (hasFile bool, err error) {
	configBytes, err := ioutil.ReadFile(configFileName)
	if err != nil {
		return false, nil
	}
	err = toml.Unmarshal(configBytes, &config)
	return true, err
}
开发者ID:rdterner,项目名称:tp,代码行数:8,代码来源:main.go

示例8: ReadMapConfigFromFile

func ReadMapConfigFromFile(filePath string) (*MapConfig, error) {
	var configData = make(map[string]interface{})
	fileBytes := MustReadFile(filePath)
	err := toml.Unmarshal(fileBytes, &configData)
	if err != nil {
		return nil, err
	}
	return NewMapConfig(configData), nil
}
开发者ID:tendermint,项目名称:go-config,代码行数:9,代码来源:config.go

示例9: Convert

func (tl tomlLoader) Convert(r io.Reader) (map[string]interface{}, error) {
	data, _ := ioutil.ReadAll(r)
	ret := make(map[string]interface{})
	err := toml.Unmarshal(data, &ret)
	if err != nil {
		return nil, err
	}

	return ret, nil
}
开发者ID:fzerorubigd,项目名称:onion,代码行数:10,代码来源:loader.go

示例10: parseToml

// parseToml performs the real JSON parsing.
func parseToml(cfg []byte) (*Config, error) {
	var out interface{}
	var err error
	if err = toml.Unmarshal(cfg, &out); err != nil {
		return nil, err
	}
	if out, err = normalizeValue(out); err != nil {
		return nil, err
	}
	return &Config{Root: out}, nil
}
开发者ID:DLag,项目名称:config,代码行数:12,代码来源:config.go

示例11: InitConfig

func InitConfig(path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	data, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}
	return toml.Unmarshal(data, &Config)
}
开发者ID:Alienero,项目名称:Rambo,代码行数:11,代码来源:config.go

示例12: I18nDataFromTOML

// I18nDataFromTOML parse toml data to map
func I18nDataFromTOML(data []byte) (map[string]map[string]string, error) {
	maps := make(map[string]map[string]string)
	if err := toml.Unmarshal(data, &maps); err != nil {
		return nil, err
	}
	for k, v := range maps {
		if len(v) == 0 {
			return nil, fmt.Errorf("i18n section '%s' is empty", k)
		}
	}
	return maps, nil
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:13,代码来源:i18n.go

示例13: Parse

// Parse the metadata
func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
	b, markdown, err := extractMetadata(t, b)
	if err != nil {
		return markdown, err
	}
	m := make(map[string]interface{})
	if err := toml.Unmarshal(b, &m); err != nil {
		return markdown, err
	}
	t.metadata.load(m)
	return markdown, nil
}
开发者ID:tgulacsi,项目名称:caddy,代码行数:13,代码来源:metadata.go

示例14: NewPostOfMarkdown

// NewPostOfMarkdown create new post from markdown file
func NewPostOfMarkdown(file string, post *Post) (*Post, error) {
	fileBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	if len(fileBytes) < 3 {
		return nil, fmt.Errorf("post content is too less")
	}

	if post == nil {
		dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
		if len(dataSlice) != 3 {
			return nil, fmt.Errorf("post need front-matter block and markdown block")
		}

		idx := getFirstBreakByte(dataSlice[1])
		if idx == 0 {
			return nil, fmt.Errorf("post need front-matter block and markdown block")
		}

		formatType := detectFormat(string(dataSlice[1][:idx]))
		if formatType == 0 {
			return nil, fmt.Errorf("post front-matter block is unrecognized")
		}

		post = new(Post)
		if formatType == FormatTOML {
			if err = toml.Unmarshal(dataSlice[1][idx:], post); err != nil {
				return nil, err
			}
		}
		if formatType == FormatINI {
			iniObj, err := ini.Load(dataSlice[1][idx:])
			if err != nil {
				return nil, err
			}
			section := iniObj.Section("DEFAULT")
			if err = newPostFromIniSection(section, post); err != nil {
				return nil, err
			}
		}
		post.Bytes = bytes.Trim(dataSlice[2], "\n")
	} else {
		post.Bytes = bytes.Trim(fileBytes, "\n")
	}
	post.fileURL = file
	if post.Date == "" {
		t, _ := com.FileMTime(file)
		post.dateTime = time.Unix(t, 0)
	}
	return post, post.normalize()
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:53,代码来源:post.go

示例15: parseSuites

func parseSuites(suites []string) (map[string]*configurationSuite, error) {
	configs := map[string]*configurationSuite{}
	for _, suite := range suites {
		logrus.Debugf("Handling suite %s", suite)
		absPath, err := filepath.Abs(suite)
		if err != nil {
			return nil, fmt.Errorf("could not resolve %s: %s", suite, err)
		}

		info, err := os.Stat(absPath)
		if err != nil {
			return nil, fmt.Errorf("error statting %s: %s", suite, err)
		}
		if info.IsDir() {
			absPath = filepath.Join(absPath, "golem.conf")
			if _, err := os.Stat(absPath); err != nil {
				return nil, fmt.Errorf("error statting %s: %s", filepath.Join(suite, "golem.conf"), err)
			}
		}

		confBytes, err := ioutil.ReadFile(absPath)
		if err != nil {
			return nil, fmt.Errorf("unable to open configuration file %s: %s", absPath, err)
		}

		// Load
		var conf suitesConfiguration
		if err := toml.Unmarshal(confBytes, &conf); err != nil {
			return nil, fmt.Errorf("error unmarshalling %s: %s", absPath, err)
		}

		logrus.Debugf("Found %d test suites in %s", len(conf.Suites), suite)
		for _, sc := range conf.Suites {
			p := filepath.Dir(absPath)
			suiteConfig, err := newSuiteConfiguration(p, sc)
			if err != nil {
				return nil, err
			}

			name := suiteConfig.Name()
			_, ok := configs[name]
			for i := 1; ok; i++ {
				name = fmt.Sprintf("%s-%d", suiteConfig.Name(), i)
				_, ok = configs[name]
			}
			suiteConfig.SetName(name)
			configs[name] = suiteConfig
		}
	}

	return configs, nil
}
开发者ID:carriercomm,项目名称:golem,代码行数:52,代码来源:configuration.go


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