tparse

package module
v0.0.0-...-9b5dae8 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2017 License: GPL-3.0 Imports: 5 Imported by: 0

README

tparse

tparse is a golang library for parsing simple toml like syntax

For Documentation visit : Link

Sample Usage

tomlStr := `[Linus Torvalds]
Found = Linux, git

[Guido Van Rossum]
Found = Python, Gerrit

[Larry Wall]
Found = Perl`
var dict *tparse.Dict = tparse.NewDict()
dict.Parse(tomlStr)
e, err := dict.Find("Linus Torvalds")
if err != nil {
  fmt.Println(err)
  return
}
found, err := e.Find("Found")
if err != nil {
  fmt.Println(err)
  return
}
fmt.Println(found)

// Output:
// Linux, git

Documentation

Overview

Package tparse implements data structures and simple functions to parse toml like syntax

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dict

type Dict map[string]Entries

Dict Type is used for containing the whole toml like data

func NewDict

func NewDict() *Dict

NewDict returns the pointer to a Dict instance after initializing it.

func (Dict) Find

func (d Dict) Find(heading string) (Entries, error)

Find returns Entries if found and an error if not for a given heading.

Example
package main

import (
	"fmt"

	"github.com/shivam07a/tparse"
)

func main() {
	tomlStr := `[Linus Torvalds]
  Found = Linux, git

  [Guido Van Rossum]
  Found = Python, Gerrit

  [Larry Wall]
  Found = Perl`
	var dict *tparse.Dict = tparse.NewDict()
	err := dict.Parse(tomlStr)
	if err != nil {
		fmt.Println(err)
	}
	e, err := dict.Find("Linus Torvalds")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(e)
}
Output:
map[Found:Linux, git]

func (Dict) Parse

func (d Dict) Parse(contents string) error

Parse is used to parse the content and store it in the Dict variable.

Example
package main

import (
	"fmt"

	"github.com/shivam07a/tparse"
)

func main() {
	tomlStr := `[Linus Torvalds]
  Found = Linux, git

  [Guido Van Rossum]
  Found = Python, Gerrit

  [Larry Wall]
  Found = Perl`
	var dict *tparse.Dict = tparse.NewDict()
	err := dict.Parse(tomlStr)
	if err != nil {
		fmt.Println(err)
	}
}

func (Dict) UnMarshal

func (d Dict) UnMarshal(w io.Writer) error

UnMarshal writes the data as a toml structured text in a io.Writer

Example
tomlStr := `[Linus Torvalds]
  Found = Linux, git

  [Guido Van Rossum]
  Found = Python, Gerrit

  [Larry Wall]
  Found = Perl`
var dict *tparse.Dict = tparse.NewDict()
err := dict.Parse(tomlStr)
if err != nil {
	fmt.Println(err)
	return
}
if err := d.UnMarshal(os.Stdout); err != nil {
	fmt.Println(err)
	return
}
Output:
[Linus Torvalds]
Found = Linux, git

[Guido Van Rossum]
Found = Python, Gerrit

[Larry Wall]
Found = Perl

type Entries

type Entries map[string]string

Entries Type is used for containing the key value pairs

func (Entries) Find

func (e Entries) Find(key string) (string, error)

Find returns value for the given string from the entries data structure. If not found it returns an empty string and an error.

Example
package main

import (
	"fmt"

	"github.com/shivam07a/tparse"
)

func main() {
	tomlStr := `[Linus Torvalds]
  Found = Linux, git

  [Guido Van Rossum]
  Found = Python, Gerrit

  [Larry Wall]
  Found = Perl`
	var dict *tparse.Dict = tparse.NewDict()
	err := dict.Parse(tomlStr)
	if err != nil {
		fmt.Println(err)
	}
	e, err := dict.Find("Linus Torvalds")
	if err != nil {
		fmt.Println(err)
		return
	}
	found, err := e.Find("Found")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(found)

}
Output:
Linux, git

Jump to

Keyboard shortcuts

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