toml

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2022 License: MIT Imports: 4 Imported by: 13

README

Go Report Card GoDoc

TOML

A TOML parser and JSON encoder.

TOML 1.0 compliant

give it a try

Installation:

go get github.com/komkom/toml

Unmarshaling a toml doc

Since the parser transforms a toml in stream into a valid json, normal json unmarshaling from the std lib can be used.

doc := `
[some]
toml="doc"`

dec := json.NewDecoder(toml.New(bytes.NewBufferString(doc)))

st := struct {
  Some struct {
    Toml string `json:"toml"`
  } `json:"some"`
}{}

err := dec.Decode(&st)
if err != nil {
  panic(err)
}
        
fmt.Printf("toml: %v\n", st.Some.Toml)

Performance Considerations

In the repo there are two benchmarks comparing throughputs of just reading data from memory versus also transforming and parsing the data. The parser slows down data throughput around 15x here. These benchmarks are by no means thorough and only hints at an estimate.

Parser Throughput    7.05 MB/s
Memory Throughput    100.03 MB/s

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/komkom/toml"
)

func main() {

	doc := `
[some]
toml="doc"
[to.map]
"!"=true`

	dec := json.NewDecoder(toml.New(bytes.NewBufferString(doc)))

	st := struct {
		Some struct {
			Toml string `json:"toml"`
		} `json:"some"`

		To struct {
			Map struct {
				IsMarked bool `json:"!"`
			}
		} `json:"to"`
	}{}

	err := dec.Decode(&st)
	if err != nil {
		panic(err)
	}

	fmt.Printf("toml: %v", st.Some.Toml)
	fmt.Printf(" is_marked: %v", st.To.Map.IsMarked)

}
Output:

toml: doc is_marked: true

func New

func New(reader io.Reader) *Reader

New wraps an io.Reader around an io.Reader. Reading data from this Reader reads data from its underlying wrapped io.Reader, parses and encodes it as a JSON stream.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL