TOML Format configuration file

Source: Internet
Author: User

TOML:

GitHub's current new project has been diverted it's coffeescript. Coffeescript is much more concise and elegant than JavaScript. Similarly, GitHub feels that YAML is not concise and elegant, so it makes a TOML.

The goal of TOML is to become a minimalist profile format. TOML is designed to be unambiguously mapped to a hash table, which is parsed by multiple languages.


Example

title = "TOML Example" [Owner]name = "Tom preston-werner" organization = "GitHub" bio = "GitHub cofounder & Ceo\nlikes Tater t OTs and Beer. " DOB = 1979-05-27t07:32:00z # Date Time is a class citizen. Why not? [Database]server = "192.168.1.1" ports = [8001, 8001, 8002]connection_max = 5000enabled = true[servers] # You can indent according to your wishes. Use a space or tab.  Toml don't care.  [Servers.alpha] IP = "10.0.0.1" dc = "EQDC10" [Servers.beta] IP = "10.0.0.2" dc = "EQDC10" [Clients]data = [["Gamma", "Delta"], [1, 2]]# is not related to line wrapping in an array. hosts = ["Alpha", "Omega"]

TOML is case-sensitive.

Func replaceenvsfile (path string)   (string, error)  {file, err := os. Open (path) if err != nil {return  "",  err}r, err := envsub (file) if  err != nil {return  "",  err}contents, err := ioutil. ReadAll (R) if err != nil {return  "",  err}return string (contents),  nil} Func envsub (R io. Reader)   (IO. Reader, error)  {bufin := bufio. Newreader (r) bufout := new (bytes. Buffer) for {chunk, err := bufin.readbytes (Byte ('% ')) if err != nil {if  err == io. Eof {// we ' Re done.bufout.write (chunk) break}return nil, err}bufout.write (Chunk[:len ( Chunk)-1]) tmp := make ([]byte, 4) Tmp, err = bufin.peek (4) if err !=  nil {if err == io. Eof {// end of file, wrIte the last few bytes out and exit.bufout.writerune ('% ') bufOut.Write (TMP) Break}return nil, err}if string (TMP)  ==  "env["  {// Found opening  delimiter, advance the read cursor and look for// closing  Delimiter.tmp, err = bufin.readbytes (Byte (' [')) If err != nil {// this  shouldn ' T happen, since the peek succeeded.return nil, err}chunk,  err = bufin.readbytes (Byte ('] ')) If err != nil {if err == io . eof {// no closing delimiter, return an errorreturn nil,  errmissingclosedelim}return nil, err}//  ' Chunk '  is now holding var  Name + closing delimiter.// var name contains invalid characters,  return an errorif  bytes. Indexany (chunk, invalidenvchars)  != -1 | | bytes. Index (Chunk, invalidenvprefix)  != -1 {return nil, errinvalidchars}varname : = string (Chunk[:len (chunk)-1]) Varval := os. Getenv (VarName) bufout.writestring (varval)} else {// just a random  '% ',  not  an opening delimiter, write it out and// keep  Going.bufOut.WriteRune ('% ')}}return bufout, nil}func main () {       contents, err := replaceenvsfile ("test.toml")  if err != nil { Return err}if _, err = toml. Decode (contents, &configfile);  err != nil {return fmt. Errorf ("error decoding config file: %s",  err)}}

Configuration file Test.toml

[Hekad]

Share_dir= "/usr/local/heka"

The output is:

Map[hekad:map[share_dir:/usr/local/heka]]


TOML Format configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.