ltsv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 8 Imported by: 9

README

go-ltsv

Test Status Coverage Status MIT License PkgGoDev

LTSV library to map ltsv to struct.

Synopsis

import (
	"net"

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

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

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)
	...
}

Description

LTSV parser and encoder for Go with reflection

Installation

% go get github.com/Songmu/go-ltsv

Author

Songmu

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal returns the LTSV encoding of v

func MarshalTo

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

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

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

Example
package main

import (
	"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)
	pretty.Println(l)
}
Output:

ltsv_test.log{
    Time:    time.Date(2016, time.July, 13, 0, 0, 4, 0, time.Location("")),
    Host:    {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xc0, 0x0, 0x2, 0x1},
    Req:     "POST /api/v0/tsdb HTTP/1.1",
    Status:  200,
    Size:    36,
    UA:      "ua:mackerel-agent/0.31.2 (Revision 775fad2)",
    ReqTime: 0.087,
    AppTime: (*float64)(nil),
    VHost:   "mackerel.io",
}

Types

type MarshalError

type MarshalError map[string]error

MarshalError is an error type for Marshal()

func (MarshalError) Error

func (m MarshalError) Error() string

func (MarshalError) OfField

func (m MarshalError) OfField(name string) error

OfField returns the error correspoinding to a given field

type MarshalTypeError

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

func (e *MarshalTypeError) Error() string

type UnmarshalError

type UnmarshalError map[string]error

UnmarshalError is an error type for Unmarshal()

func (UnmarshalError) Error

func (m UnmarshalError) Error() string

func (UnmarshalError) OfField

func (m UnmarshalError) OfField(name string) error

OfField returns the error correspoinding to a given field

type UnmarshalTypeError

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

func (e *UnmarshalTypeError) Error() string

Jump to

Keyboard shortcuts

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