gou

package
v0.0.0-...-e1c5934 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2015 License: MIT, Apache-2.0 Imports: 20 Imported by: 0

README

gou - Go Utilities

Go Utilities (logging, json)

JsonHelper

A Go Json Helper, focused on Type coercion, and json path query.

	package main
	import . "github.com/araddon/gou"
	import . "github.com/araddon/gou/goutest"
	import "testing"


	func TestJsonHelper() {

		var jsonData := []byte(`{
			"name":"aaron",
			"nullstring":null,
			"ints":[1,2,3,4],
			"int":1,
			"intstr":"1",
			"int64":1234567890,
			"MaxSize" : 1048576,
			"strings":["string1"],
			"stringscsv":"string1,string2",
			"nested":{
				"nest":"string2",
				"strings":["string1"],
				"int":2,
				"list":["value"],
				"nest2":{
					"test":"good"
				}
			},
			"nested2":[
				{"sub":2}
			],
			"period.name":"value"
		}`

		jh := NewJsonHelper(jsonData)

		// String method
		Assert(jh.String("name") == "aaron", t, "should get 'aaron' %s", jh.String("name"))
		// Int Method
		Assert(jh.Int("int") == 1, t, "get int ")
		// Selecting items from an array
		Assert(jh.Int("ints[0]") == 1, t, "get int from array %d", jh.Int("ints[0]"))
		Assert(jh.Int("ints[2]") == 3, t, "get int from array %d", jh.Int("ints[0]"))
		// Getting arrays
		Assert(len(jh.Ints("ints")) == 4, t, "get int array %v", jh.Ints("ints"))
		// Type coercion to Int64
		Assert(jh.Int64("int64") == 1234567890, t, "get int")
		Assert(jh.Int("nested.int") == 2, t, "get int")

		// Path based selection
		Assert(jh.String("nested.nest") == "string2", t, "should get string %s", jh.String("nested.nest"))
		Assert(jh.String("nested.nest2.test") == "good", t, "should get string %s", jh.String("nested.nest2.test"))
		Assert(jh.String("nested.list[0]") == "value", t, "get string from array")
		Assert(jh.Int("nested2[0].sub") == 2, t, "get int from obj in array %d", jh.Int("nested2[0].sub"))

		// casing?
		Assert(jh.Int("MaxSize") == 1048576, t, "get int, test capitalization? ")
		sl := jh.Strings("strings")
		Assert(len(sl) == 1 && sl[0] == "string1", t, "get strings ")
		sl = jh.Strings("stringscsv")
		Assert(len(sl) == 2 && sl[0] == "string1", t, "get strings ")

		// Safe gets
		i64, ok := jh.Int64Safe("int64")
		Assert(ok, t, "int64safe ok")
		Assert(i64 == 1234567890, t, "int64safe value")

		i, ok := jh.IntSafe("int")
		Assert(ok, t, "intsafe ok")
		Assert(i == 1, t, "intsafe value")

		l := jh.List("nested2")
		Assert(len(l) == 1, t, "get list")

		jhm := jh.Helpers("nested2")
		Assert(len(jhm) == 1, t, "get list of helpers")
		Assert(jhm[0].Int("sub") == 2, t, "Should get list of helpers")

		// Now lets test xpath type syntax
		Assert(jh.Int("/MaxSize") == 1048576, t, "get int, test capitalization? ")
		Assert(jh.String("/nested/nest") == "string2", t, "should get string %s", jh.String("/nested/nest"))
		Assert(jh.String("/nested/list[0]") == "value", t, "get string from array")
		// note this one has period in name
		Assert(jh.String("/period.name") == "value", t, "test period in name ")
	}

Logging

Yet Another Go Logger, configureable logging.

	package main
	import "github.com/araddon/gou"
	import "flag"

	var logLevel *string = flag.String("logging", "debug", "Which log level: [debug,info,warn,error,fatal]")

	func main() {

		flag.Parse()
		gou.SetupLogging(*logLevel)

		// logging methods
		gou.Debug("hello", thing, " more ", stuff)

		gou.Error("hello")

		gou.Errorf("hello %v", thing)
	}

License

MIT License

Documentation

Index

Constants

View Source
const (
	MaxInt  = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1
	MinInt  = -MaxInt - 1            // either -1 << 31 or -1 << 63
	MaxUint = 1<<BitsPerWord - 1     // either 1<<32 - 1 or 1<<64 - 1
)

Implementation-specific integer limit values.

View Source
const (
	NOLOGGING = -1
	FATAL     = 0
	ERROR     = 1
	WARN      = 2
	INFO      = 3
	DEBUG     = 4
)
View Source
const BitsPerWord = bitsPerWord // either 32 or 64

Implementation-specific size of int and uint in bits.

Variables

View Source
var (
	LogLevel    int = ERROR
	EMPTY       struct{}
	ErrLogLevel int = ERROR

	LogColor = map[int]string{FATAL: "\033[0m\033[37m",
		ERROR: "\033[0m\033[31m",
		WARN:  "\033[0m\033[33m",
		INFO:  "\033[0m\033[35m",
		DEBUG: "\033[0m\033[34m"}
	LogPrefix = map[int]string{
		FATAL: "[FATAL] ",
		ERROR: "[ERROR] ",
		WARN:  "[WARN] ",
		INFO:  "[INFO] ",
		DEBUG: "[DEBUG] ",
	}

	LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1}
)

Functions

func CloseEnuf

func CloseEnuf(a, b float64) bool

take two floats, compare, need to be within 2%

func CloseInt

func CloseInt(a, b int) bool

take two ints, compare, need to be within 5%

func CoerceFloat

func CoerceFloat(v interface{}) (float64, error)

func CoerceFloatShort

func CoerceFloatShort(v interface{}) float64

func CoerceInt

func CoerceInt(v interface{}) (int, error)

func CoerceInt64

func CoerceInt64(v interface{}) (int64, error)

func CoerceInt64Short

func CoerceInt64Short(v interface{}) int64

func CoerceIntShort

func CoerceIntShort(v interface{}) int

func CoerceString

func CoerceString(v interface{}) (string, error)

Coerce types (string,int,int64, float, []byte) into String type

func CoerceStringShort

func CoerceStringShort(v interface{}) string

Coerce type to string, returning zero length string if error or nil

func CoerceUint

func CoerceUint(v interface{}) (uint64, error)

Coerce a val(interface{}) into a Uint64

func CoerceUintShort

func CoerceUintShort(v interface{}) uint64

Coerce a Val(interface{}) into Uint64

func Debug

func Debug(v ...interface{})

Log at debug level

func Debugf

func Debugf(format string, v ...interface{})

Debug log formatted

func DeleteJson

func DeleteJson(url, body string) (ret string, err error, resp *http.Response)

issues http delete an application/json to url with body

func DiscardStandardLogger

func DiscardStandardLogger()

Setup default log output to go to a dev/null

log.SetOutput(new(DevNull))

func DoLog

func DoLog(depth, logLvl int, msg string)

Low level log with depth , level, message and logger

func Error

func Error(v ...interface{})

Log at error level

func Errorf

func Errorf(format string, v ...interface{})

Error log formatted

func Fetch

func Fetch(url string) (ret []byte, err error)

Simple Fetch Wrapper, given a url it returns bytes

func FetchResp

func FetchResp(url string) (ret []byte, err error, resp *http.Response)

Simple Fetch Wrapper, given a url it returns bytes and response

func GetErrLogger

func GetErrLogger() *log.Logger

func GetLogger

func GetLogger() *log.Logger

func Info

func Info(v ...interface{})

Log at info level

func Infof

func Infof(format string, v ...interface{})

info log formatted

func IsTerminal

func IsTerminal() bool

Determine is this process is running in a Terminal or not?

func JsonString

func JsonString(v interface{}) string

func Log

func Log(logLvl int, v ...interface{})

Log to logger if setup

Log(ERROR, "message")

func LogD

func LogD(depth int, logLvl int, v ...interface{})

When you want to use the log short filename flag, and want to use the lower level logging functions (say from an *Assert* type function) you need to modify the stack depth:

    func init() {}
	       SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile|log.Lmicroseconds), lvl)
    }

    func assert(t *testing.T, myData) {
        // we want log line to show line that called this assert, not this line
        LogD(5, DEBUG, v...)
    }

func LogLevelSet

func LogLevelSet(levelWord string)

sets the log level from a string

func LogP

func LogP(logLvl int, prefix string, v ...interface{})

Log to logger if setup

LogP(ERROR, "prefix", "message", anyItems, youWant)

func LogPf

func LogPf(logLvl int, prefix string, format string, v ...interface{})

Log to logger if setup with a prefix

LogPf(ERROR, "prefix", "formatString %s %v", anyItems, youWant)

func LogTracef

func LogTracef(logLvl int, format string, v ...interface{})

Log to logger if setup, grab a stack trace and add that as well

u.LogTracef(u.ERROR, "message %s", varx)

func Logf

func Logf(logLvl int, format string, v ...interface{})

Log to logger if setup

Logf(ERROR, "message %d", 20)

func MakeJsonList

func MakeJsonList(b []byte) []byte

Convert a slice of bytes into an array by ensuring it is wrapped

with []

func NewUid

func NewUid() uint64

Create a new uint64 unique id

func PostForm

func PostForm(url, body string) (ret string, err error, resp *http.Response)

posts a www-form encoded form to url with body

func PostJson

func PostJson(url, body string) (ret string, err error, resp *http.Response)

posts an application/json to url with body ie: type = application/json

func PutJson

func PutJson(url, body string) (ret string, err error, resp *http.Response)

issues http put an application/json to url with optional body

func SetColorIfTerminal

func SetColorIfTerminal()

Setup colorized output if this is a terminal

func SetColorOutput

func SetColorOutput()

Setup colorized output

func SetErrLogger

func SetErrLogger(l *log.Logger, logLevel string)

you can set a logger, and log level. this is for errors, and assumes you are logging to Stderr (seperate from stdout above), allowing you to seperate debug&info logging from errors

	gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags), "debug")

 loglevls:   debug, info, warn, error, fatal

func SetLogger

func SetLogger(l *log.Logger, logLevel string)

you can set a logger, and log level,most common usage is:

	gou.SetLogger(log.New(os.Stdout, "", log.LstdFlags), "debug")

 loglevls:   debug, info, warn, error, fatal

Note, that you can also set a seperate Error Log Level

func SetStopper

func SetStopper(f func())

Use this in combo with StopCheck() for test functions that must start processes such as

func SetupLogging

func SetupLogging(lvl string)

Setup default logging to Stderr, equivalent to:

gou.SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile), "debug")

func SetupLoggingLong

func SetupLoggingLong(lvl string)

Setup default logging to Stderr, equivalent to:

gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), level)

func StartTest

func StartTest()

func StopCheck

func StopCheck()

func WaitFor

func WaitFor(check func() bool, timeoutSecs int)

Wait for condition (defined by func) to be true this is mostly for testing, but a utility to create a ticker checking back every 100 ms to see if something (the supplied check func) is done

WaitFor(func() bool {
   return ctr.Ct == 0
},10)

timeout (in seconds) is the last arg

func Warn

func Warn(v ...interface{})

Log at warn level

func Warnf

func Warnf(format string, v ...interface{})

Debug log formatted

Types

type DevNull

type DevNull struct{}

http://play.golang.org/p/5LIA41Iqfp Dummy discard, satisfies io.Writer without importing io or os.

func (DevNull) Write

func (DevNull) Write(p []byte) (int, error)

type JsonHelper

type JsonHelper map[string]interface{}

A wrapper around a map[string]interface{} to facilitate coercion of json data to what you want

allows usage such as this

jh := NewJsonHelper([]byte(`{
	"name":"string",
	"ints":[1,5,9,11],
	"int":1,
	"int64":1234567890,
	"MaxSize" : 1048576,
	"strings":["string1"],
	"nested":{
		"nest":"string2",
		"strings":["string1"],
		"int":2,
		"list":["value"],
		"nest2":{
			"test":"good"
		}
	},
	"nested2":[
		{"sub":5}
	]
}`)

i := jh.Int("nested.int")  // 2
i2 := jh.Int("ints[1]")    // 5   array position 1 from [1,5,9,11]
s := jh.String("nested.nest")  // "string2"

func JsonHelperHttp

func JsonHelperHttp(method, urlStr string, data interface{}) (JsonHelper, error)

Simple Fetch Wrapper, given a url it returns Helper, error Sends as type application/json, interprets whatever datatype is sent in appropriately

func NewJsonHelper

func NewJsonHelper(b []byte) JsonHelper

func NewJsonHelperFromResp

func NewJsonHelperFromResp(resp *http.Response) (JsonHelper, error)

Make a JsonHelper from http response. This will automatically close the response body

func NewJsonHelperReader

func NewJsonHelperReader(r io.Reader) (jh JsonHelper, err error)

func NewJsonHelpers

func NewJsonHelpers(b []byte) []JsonHelper

func (JsonHelper) Bool

func (j JsonHelper) Bool(n string) bool

func (JsonHelper) BoolSafe

func (j JsonHelper) BoolSafe(n string) (val bool, ok bool)

func (JsonHelper) Float64

func (j JsonHelper) Float64(n string) float64

func (JsonHelper) Get

func (j JsonHelper) Get(n string) interface{}

Get the key (or keypath) value as interface, mostly used internally through String, etc methods

jh.Get("name.subname")
jh.Get("name/subname")
jh.Get("name.arrayname[1]")
jh.Get("name.arrayname[]")

func (JsonHelper) Helper

func (j JsonHelper) Helper(n string) JsonHelper

Get a Helper from a string path

func (JsonHelper) Helpers

func (j JsonHelper) Helpers(n string) []JsonHelper

Get list of Helpers at given name. Trys to coerce into proper Helper type

func (JsonHelper) Int

func (j JsonHelper) Int(n string) int

func (JsonHelper) Int64

func (j JsonHelper) Int64(n string) int64

func (JsonHelper) Int64Safe

func (j JsonHelper) Int64Safe(n string) (int64, bool)

func (JsonHelper) IntSafe

func (j JsonHelper) IntSafe(n string) (int, bool)

func (JsonHelper) Ints

func (j JsonHelper) Ints(n string) []int

func (JsonHelper) Keys

func (j JsonHelper) Keys() []string

func (JsonHelper) List

func (j JsonHelper) List(n string) []interface{}

Gets slice of interface{}

func (JsonHelper) Map

func (j JsonHelper) Map(n string) map[string]interface{}

func (JsonHelper) MapSafe

func (j JsonHelper) MapSafe(n string) (map[string]interface{}, bool)

func (JsonHelper) PrettyJson

func (j JsonHelper) PrettyJson() []byte

func (JsonHelper) String

func (j JsonHelper) String(n string) string

func (JsonHelper) StringSafe

func (j JsonHelper) StringSafe(n string) (string, bool)

func (JsonHelper) Strings

func (j JsonHelper) Strings(n string) []string

func (JsonHelper) Uint64

func (j JsonHelper) Uint64(n string) uint64

type JsonInterface

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

A simple wrapper to help json data be consumed when not using Strongly typed structs.

func (*JsonInterface) Encode

func (j *JsonInterface) Encode() ([]byte, error)

Encode returns its marshaled data as `[]byte`

func (*JsonInterface) Float

func (j *JsonInterface) Float() (float32, error)

Coerce to Float, return err if needed

func (JsonInterface) FloatSh

func (j JsonInterface) FloatSh() float32

Coerce to Float, 0 returned if 0 or missing

func (*JsonInterface) Int

func (j *JsonInterface) Int() (int, error)

Coerce to Int

func (JsonInterface) IntSh

func (j JsonInterface) IntSh() int

Coerce to Int, 0 returned if missing or zero

func (*JsonInterface) MarshalJSON

func (j *JsonInterface) MarshalJSON() ([]byte, error)

Implements the json.Marshaler interface.

func (*JsonInterface) String

func (j *JsonInterface) String() (string, error)

Coerce to a String

func (JsonInterface) StringSh

func (j JsonInterface) StringSh() string

Coerce to a string, may be zero length if missing, or zero length

func (*JsonInterface) UnmarshalJSON

func (j *JsonInterface) UnmarshalJSON(raw []byte) error

Implements the json.Unmarshal interface.

type JsonRawWriter

type JsonRawWriter struct {
	bytes.Buffer
}

func (*JsonRawWriter) MarshalJSON

func (m *JsonRawWriter) MarshalJSON() ([]byte, error)

func (*JsonRawWriter) Raw

func (m *JsonRawWriter) Raw() json.RawMessage

type Uid

type Uid uint64

uid is a 64 bit int uid

func (*Uid) String

func (u *Uid) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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