go-ltsv: github.com/Songmu/go-ltsv Index | Examples | Files

package ltsv

import "github.com/Songmu/go-ltsv"

Index

Examples

Package Files

decode.go encode.go

func Marshal Uses

func Marshal(v interface{}) ([]byte, error)

Marshal returns the LTSV encoding of v

func MarshalTo Uses

func MarshalTo(w io.Writer, v interface{}) error

MarshalTo writes the LTSV encoding of v into w. Be aware that the writing into w is not thread safe.

func Unmarshal Uses

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the LTSV-encoded data and stores the result in the value pointed to by v.

Code:

package main

import (
    "fmt"
    "net"
    "time"

    "github.com/Songmu/go-ltsv"
    "github.com/kr/pretty"
)

type log struct {
    Time    *logTime
    Host    net.IP
    Req     string
    Status  int
    Size    int
    UA      string
    ReqTime float64
    AppTime *float64
    VHost   string
}

const timeFormat = "2006-01-02T15:04:05Z07:00"

type logTime struct {
    time.Time
}

func (lt *logTime) UnmarshalText(t []byte) error {
    ti, err := time.ParseInLocation(timeFormat, string(t), time.UTC)
    if err != nil {
        return err
    }
    lt.Time = ti
    return nil
}

func main() {
    ltsvLog := "time:2016-07-13T00:00:04+09:00\t" +
        "host:192.0.2.1\t" +
        "req:POST /api/v0/tsdb HTTP/1.1\t" +
        "status:200\t" +
        "size:36\t" +
        "ua:ua:mackerel-agent/0.31.2 (Revision 775fad2)\t" +
        "reqtime:0.087\t" +
        "vhost:mackerel.io"
    l := log{}
    ltsv.Unmarshal([]byte(ltsvLog), &l)
    t := l.Time
    l.Time = nil
    fmt.Println(t)
    pretty.Println(l)
}

type MarshalError Uses

type MarshalError map[string]error

MarshalError is an error type for Marshal()

func (MarshalError) Error Uses

func (m MarshalError) Error() string

func (MarshalError) OfField Uses

func (m MarshalError) OfField(name string) error

OfField returns the error correspoinding to a given field

type MarshalTypeError Uses

type MarshalTypeError struct {
    Value string
    Type  reflect.Type
    // contains filtered or unexported fields
}

An MarshalTypeError describes a LTSV value that was not appropriate for a value of a specific Go type.

func (*MarshalTypeError) Error Uses

func (e *MarshalTypeError) Error() string

type UnmarshalError Uses

type UnmarshalError map[string]error

UnmarshalError is an error type for Unmarshal()

func (UnmarshalError) Error Uses

func (m UnmarshalError) Error() string

func (UnmarshalError) OfField Uses

func (m UnmarshalError) OfField(name string) error

OfField returns the error correspoinding to a given field

type UnmarshalTypeError Uses

type UnmarshalTypeError struct {
    Value string
    Type  reflect.Type
}

An UnmarshalTypeError describes a LTSV value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error Uses

func (e *UnmarshalTypeError) Error() string

Package ltsv imports 8 packages (graph) and is imported by 1 packages. Updated 2017-07-24. Refresh now. Tools for package owners.