當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。