parameters

package module
v0.0.0-...-f049be8 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2014 License: MIT Imports: 7 Imported by: 0

README

go-text-parameters

GoDoc Build Status Coverage Status

Encoding and decoding of text/parameters written by Go

Description

text/parameters is consists of either a list of parameters or a list of parameters and associated values. Each entry of the list is a single line of text, and parameters are separated from values by a colon.

This package provides a read and write function of text/parameters format text from your Go programs.

Spec

Usage

Decoder
package main

import (
	"fmt"
	"strings"

	"github.com/gongo/text-parameters"
)

func main() {
	body := "Name: Wataru MIYAGUNI\nlogin: gongo\nAge: 30\n"

	u := struct {
		Name    string
		LoginId string `parameters:"login"`
		Age     int
	}{}

	decoder := parameters.NewDecorder(strings.NewReader(body))
	decoder.Decode(&u)

	fmt.Println("u.Name    =", u.Name)
	fmt.Println("u.LoginId =", u.LoginId)
	fmt.Println("u.Age     =", u.Age)
}

Output:

u.Name    = Wataru MIYAGUNI
u.LoginId = gongo
u.Age     = 30
Encoder
package main

import (
	"bytes"
	"fmt"

	"github.com/gongo/text-parameters"
)

func main() {
	var body bytes.Buffer

	u := struct {
		Name    string
		LoginId string `parameters:"login_id"`
		Rate    float64
	}{
		Name:    "Wataru MIYAGUNI",
		LoginId: "gongo",
		Rate:    0.923,
	}

	encoder := parameters.NewEncoder(&body)
	encoder.Encode(&u)

	fmt.Println(body.String())
}

Output:

Name: Wataru MIYAGUNI
Rate: 0.923
login_id: gongo

Install

$ go get github.com/gongo/text-parameters

LICENSE

MIT License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(tp *TextParameters) string
Example
params := &parameters.TextParameters{
	"Foo": "foobar",
	"Bar": "golang",
}

encoded := parameters.Marshal(params)
fmt.Println(encoded)
Output:
Bar: golang
Foo: foobar

Types

type CodingStructPointerError

type CodingStructPointerError struct {
	// contains filtered or unexported fields
}

func (*CodingStructPointerError) Error

func (e *CodingStructPointerError) Error() string

type DecodeFieldTypeError

type DecodeFieldTypeError struct {
	// contains filtered or unexported fields
}

func (*DecodeFieldTypeError) Error

func (e *DecodeFieldTypeError) Error() string

type DecodeFormatError

type DecodeFormatError struct {
	// contains filtered or unexported fields
}

func (*DecodeFormatError) Error

func (e *DecodeFormatError) Error() string

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}
Example
body := `Foo: 3.14
barbaz: golang
Piyo: 123`

s := struct {
	Foo  float64
	Bar  string `parameters:"barbaz"`
	Piyo int
}{}

decoder := parameters.NewDecorder(strings.NewReader(body))
decoder.Decode(&s)

fmt.Println("s.Foo  =", s.Foo)
fmt.Println("s.Bar  =", s.Bar)
fmt.Println("s.Piyo =", s.Piyo)
Output:
s.Foo  = 3.14
s.Bar  = golang
s.Piyo = 123

func NewDecorder

func NewDecorder(r io.Reader) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}
Example
s := struct {
	Foo  float64
	Piyo int
	Bar  string `parameters:"barbaz"`
}{
	Foo:  1.41421356,
	Piyo: 12345,
	Bar:  "golang",
}

var b bytes.Buffer
encoder := parameters.NewEncoder(&b)
encoder.Encode(&s)

fmt.Print(b.String())
Output:
Foo: 1.41421356
Piyo: 12345
barbaz: golang

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

type TextParameters

type TextParameters map[string]string

func Unmarshal

func Unmarshal(r io.Reader) (*TextParameters, error)
Example
body := `foo: 3
bar: 4
baz: 5`

obj, _ := parameters.Unmarshal(strings.NewReader(body))

fmt.Println(obj.Get("foo"), obj.Get("bar"), obj.Get("baz"))
Output:
3 4 5

func (TextParameters) Del

func (t TextParameters) Del(key string)

func (TextParameters) Get

func (t TextParameters) Get(key string) string

func (TextParameters) Keys

func (t TextParameters) Keys() []string

func (TextParameters) Set

func (t TextParameters) Set(key, value string)

Jump to

Keyboard shortcuts

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