From cf2989a2c88e304366e2ce7bfa194c9cbf4b670b Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Sun, 29 Nov 2015 19:57:46 +0100 Subject: [PATCH] add Go Report Card badge to readme gometalinter changes --- .gitignore | 2 + README.md | 1 + ansi/ansi.go | 9 +++- as/as.go | 3 +- as/as_test.go | 38 ++++++++--------- cache/cache.go | 10 ++++- dump/dump.go | 97 -------------------------------------------- dump/dump_test.go | 66 ------------------------------ dump/example_test.go | 41 ------------------- node/node.go | 36 ++++++++-------- 10 files changed, 58 insertions(+), 245 deletions(-) delete mode 100755 dump/dump.go delete mode 100755 dump/dump_test.go delete mode 100755 dump/example_test.go diff --git a/.gitignore b/.gitignore index a4e5a04..3e4979e 100755 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ # Folders _obj _test +.vscode +-trash # Architecture specific extensions/prefixes *.[568vq] diff --git a/README.md b/README.md index 9ed6453..b2f1365 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ go test ./... ![Windows Build Status](https://simonwaldherr.de/icon/win.png) | [![Build status](https://img.shields.io/appveyor/ci/SimonWaldherr/golibs.svg?style=flat-square)](https://ci.appveyor.com/project/SimonWaldherr/golibs/branch/master) | appveyor.com | test under windows ![Linux Build Status](https://simonwaldherr.de/icon/tux.png) | [![Build status](https://magnum-ci.com/status/e9ccc5689f4135e4021475bfdf0cf527.png)](https://magnum-ci.com/public/c4dba43a1c41dbff557e/builds) | magnum-ci.com | just another ci service ![Linux Build Status](https://simonwaldherr.de/icon/tux1.png) | [![Build Status](https://semaphoreci.com/api/v1/projects/fe1a7a53-a2c0-4bc0-a539-4af4ef13d49f/487313/shields_badge.svg)](https://semaphoreci.com/simonwaldherr/golibs) | semaphoreci.com | yet another ci service + | [![Go Report Card](http://goreportcard.com/badge/simonwaldherr/golibs)](http://goreportcard.com/report/simonwaldherr/golibs) | goreportcard.com | report card | [![Coverage Status](https://img.shields.io/coveralls/SimonWaldherr/golibs.svg?style=flat-square)](https://coveralls.io/r/SimonWaldherr/golibs) | coveralls.io | test coverage | [![codecov.io](http://codecov.io/github/SimonWaldherr/golibs/coverage.svg?branch=master)](http://codecov.io/github/SimonWaldherr/golibs?branch=master) | codecov.io | test coverage | [![Coverage Status](https://img.shields.io/coveralls/SimonWaldherr/golibs.svg?style=flat-square)](https://simonwaldherr.de/gocover/golibs/) | | go tool cover diff --git a/ansi/ansi.go b/ansi/ansi.go index e6a5d0e..ab64668 100755 --- a/ansi/ansi.go +++ b/ansi/ansi.go @@ -1,4 +1,4 @@ -// with this package you can print colored and styled text to your terminal. +// Package ansi can print colored and styled text to your terminal. package ansi import ( @@ -6,6 +6,7 @@ import ( "log" ) +// Col defines supported colors type Col int // defines supported colors @@ -19,6 +20,8 @@ const ( Cyan White ) + +// defines text styling options const ( TReset = Col(iota) TBold @@ -56,6 +59,7 @@ const ( BgWhite ) +// Set sets styling options on strings and stringable interfaces func Set(str interface{}, Attribute ...Col) string { var rstr = fmt.Sprint(str) for _, attr := range Attribute { @@ -69,7 +73,7 @@ func Color(str interface{}, col Col) string { return fmt.Sprintf("\033[3%vm%v\033[0m", col, str) } -// Color adds the color code of col as background color to str and returns as string +// BgColor adds the color code of col as background color to str and returns as string func BgColor(str interface{}, col Col) string { return fmt.Sprintf("\033[4%vm%v\033[0m", col, str) } @@ -84,6 +88,7 @@ func Underline(str interface{}) string { return fmt.Sprintf("\033[4m%v\033[0m", str) } +// Log prints red text via log package func Log(valuea ...interface{}) interface{} { if valuea[1] != nil { log.Println(Color(valuea[1], Red)) diff --git a/as/as.go b/as/as.go index cead86b..c8de6cd 100755 --- a/as/as.go +++ b/as/as.go @@ -1,4 +1,4 @@ -// The "as" package helps by converting various data types to various other types +// Package as helps by converting various data types to various other types package as import ( @@ -469,6 +469,7 @@ func DBType(str string) string { } } +// DBTypeMultiple returns the lowest common denominator of a type for all inserted DBTypes func DBTypeMultiple(val []string) string { var typeint int for _, typ := range val { diff --git a/as/as_test.go b/as/as_test.go index 8c87bcc..25a31c0 100755 --- a/as/as_test.go +++ b/as/as_test.go @@ -305,7 +305,7 @@ func Test_Uint(t *testing.T) { } } -func println_type(t *testing.T, str, type1, type2 string) { +func printlnType(t *testing.T, str, type1, type2 string) { t1, err := Type(str) if err != nil { @@ -323,24 +323,24 @@ func println_type(t *testing.T, str, type1, type2 string) { } func Test_Type(t *testing.T) { - println_type(t, "true", "bool", "bool") - println_type(t, "1000", "int", "int") - println_type(t, "2^16", "int", "int") - println_type(t, "10E24", "int", "int") - println_type(t, "10 EUR", "price", "string") - println_type(t, "10.23 £", "price", "string") - println_type(t, "01.01.1970", "date", "string") - println_type(t, "06.06.1989", "date", "string") - println_type(t, "2015-05-04T13:37:42", "date", "string") - println_type(t, "mail@example.tld", "email", "string") - println_type(t, "978-3499626005", "isbn", "string") - println_type(t, "127.0.0.1", "ipv4", "string") - println_type(t, "2a01:4f8:192:34aa::10", "ipv6", "string") - println_type(t, "b8:c7:5d:c6:6c:c6", "mac", "string") - println_type(t, "#fefefe", "color", "string") - println_type(t, "THk4Z1JFSlVlWEJsSUhKbGRIVnlibk1nWVNCRVlYUmhZbUZ6WlNCVWVYQmxJRzltSUdFZ2MzUnlhVzVuTGcwS1puVnVZeUJFUWxSNWNHVW9jM1J5SUhOMGNtbHVaeWtnYzNSeWFXNW5JSHNOQ2dsMExDQmxjbklnT2owZ1ZIbHdaU2h6ZEhJcERRb0phV1lnWlhKeUlDRTlJRzVwYkNCN0RRb0pDWEpsZEhWeWJpQWljM1J5YVc1bklnMEtDWDBOQ2dsemQybDBZMmdnZENCN0RRb0pZMkZ6WlNBaVltOXZiQ0lzSUNKcGJuUWlMQ0FpYzNSeWFXNW5JaXdnSW1ac2IyRjBJam9OQ2drSmNtVjBkWEp1SUhRTkNnbGtaV1poZFd4ME9nMEtDUWx5WlhSMWNtNGdJbk4wY21sdVp5SU5DZ2w5RFFwOQ==", "base64", "string") - println_type(t, "!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~ ", "string", "string") - println_type(t, "😃", "", "string") + printlnType(t, "true", "bool", "bool") + printlnType(t, "1000", "int", "int") + printlnType(t, "2^16", "int", "int") + printlnType(t, "10E24", "int", "int") + printlnType(t, "10 EUR", "price", "string") + printlnType(t, "10.23 £", "price", "string") + printlnType(t, "01.01.1970", "date", "string") + printlnType(t, "06.06.1989", "date", "string") + printlnType(t, "2015-05-04T13:37:42", "date", "string") + printlnType(t, "mail@example.tld", "email", "string") + printlnType(t, "978-3499626005", "isbn", "string") + printlnType(t, "127.0.0.1", "ipv4", "string") + printlnType(t, "2a01:4f8:192:34aa::10", "ipv6", "string") + printlnType(t, "b8:c7:5d:c6:6c:c6", "mac", "string") + printlnType(t, "#fefefe", "color", "string") + printlnType(t, "THk4Z1JFSlVlWEJsSUhKbGRIVnlibk1nWVNCRVlYUmhZbUZ6WlNCVWVYQmxJRzltSUdFZ2MzUnlhVzVuTGcwS1puVnVZeUJFUWxSNWNHVW9jM1J5SUhOMGNtbHVaeWtnYzNSeWFXNW5JSHNOQ2dsMExDQmxjbklnT2owZ1ZIbHdaU2h6ZEhJcERRb0phV1lnWlhKeUlDRTlJRzVwYkNCN0RRb0pDWEpsZEhWeWJpQWljM1J5YVc1bklnMEtDWDBOQ2dsemQybDBZMmdnZENCN0RRb0pZMkZ6WlNBaVltOXZiQ0lzSUNKcGJuUWlMQ0FpYzNSeWFXNW5JaXdnSW1ac2IyRjBJam9OQ2drSmNtVjBkWEp1SUhRTkNnbGtaV1poZFd4ME9nMEtDUWx5WlhSMWNtNGdJbk4wY21sdVp5SU5DZ2w5RFFwOQ==", "base64", "string") + printlnType(t, "!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~ ", "string", "string") + printlnType(t, "😃", "", "string") } func Test_TypeMultiple(t *testing.T) { diff --git a/cache/cache.go b/cache/cache.go index 414679a..2c13877 100755 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,4 +1,4 @@ -// simple caching with GC +// Package cache simplifies caching with GC package cache import ( @@ -10,6 +10,7 @@ import ( "time" ) +// Item as a single object with an interface as value and two time.Time values for creation and expiration dates type Item struct { Object interface{} Creation time.Time @@ -23,6 +24,7 @@ func (item *Item) isExpired() bool { return item.Expiration.Before(time.Now()) } +// Cache is a storage for all Item items type Cache struct { Expiration time.Duration items map[string]*Item @@ -43,6 +45,7 @@ func (cache *Cache) String() string { return str } +// Set creates an Item in the cache, if there is already an item with that name it get overwritten func (cache *Cache) Set(key string, value interface{}) { cache.items[key] = &Item{ Object: value, @@ -51,6 +54,7 @@ func (cache *Cache) Set(key string, value interface{}) { } } +// SetWithDuration does the same as Set but with an specific expiration date func (cache *Cache) SetWithDuration(key string, value interface{}, creation time.Time, duration time.Duration) { cache.items[key] = &Item{ Object: value, @@ -59,10 +63,12 @@ func (cache *Cache) SetWithDuration(key string, value interface{}, creation time } } +// Time returns the creation date of a cached item func (cache *Cache) Time(key string) time.Time { return cache.items[key].Creation } +// Get returns the value of a cached item or nil if expired func (cache *Cache) Get(key string) interface{} { item, ok := cache.items[key] if !ok || item.isExpired() { @@ -71,10 +77,12 @@ func (cache *Cache) Get(key string) interface{} { return item.Object } +// Delete deletes a cached item func (cache *Cache) Delete(key string) { delete(cache.items, key) } +// Add creates an cached item func (cache *Cache) Add(key string, value interface{}) bool { item := cache.Get(key) if item != nil { diff --git a/dump/dump.go b/dump/dump.go deleted file mode 100755 index b5833c0..00000000 --- a/dump/dump.go +++ /dev/null @@ -1,97 +0,0 @@ -package dump - -import ( - "fmt" - "simonwaldherr.de/go/golibs/node" - "simonwaldherr.de/go/golibs/stack" -) - -func nodeWalker(obj *interface{}) string { - var str string - var ldepth int - var ltype string - - array := stack.Lifo() - - node.Node(obj, func(key *string, index *int, value *interface{}, depth int) { - if ldepth > depth { - for i := 1; i <= ldepth; i++ { - for j := 0; j < ldepth+1-i; j++ { - str += fmt.Sprint(" ") - } - str += fmt.Sprintln(array.Pop().(string)) - } - } else if depth > ldepth { - if key != nil { - if ltype == "[" { - str += fmt.Sprintf("%q {\n", *key) - } else { - str += fmt.Sprintf("{\n") - } - array.Push("}") - } else if index != nil { - str += fmt.Sprintf("[\n") - } - } - for i := 0; i <= depth; i++ { - str += fmt.Sprint(" ") - } - - v := *value - switch v.(type) { - case map[string]interface{}: - ltype = "{" - case []interface{}: - if key != nil { - str += fmt.Sprintf("%q => ", *key) - array.Push("]") - } else { - str += fmt.Sprintf("%v => ", *index) - array.Push("]") - } - ltype = "[" - case string: - if key != nil { - str += fmt.Sprintf("%q => %#v\n", *key, *value) - } else { - str += fmt.Sprintf("%d => %#v\n", *index, *value) - } - default: - if key != nil { - str += fmt.Sprintf("%q => ", *key) - } else if value != nil { - str += fmt.Sprintf("%d => %v\n", *index, *value) - } - } - ldepth = depth - }) - - for array.Len() > 0 { - for i := 0; i < array.Len(); i++ { - str += " " - } - str += array.Pop().(string) + "\n" - } - - o := *obj - - switch o.(type) { - case []interface{}: - str = "[\n" + str + "]\n" - case map[string]interface{}: - str = "{\n" + str + "}\n" - } - return str -} - -func Sprint(obj interface{}) string { - return nodeWalker(&obj) -} - -func Print(obj interface{}) { - fmt.Print(Sprint(obj)) -} - -func Println(obj interface{}) { - fmt.Println(Sprint(obj)) -} diff --git a/dump/dump_test.go b/dump/dump_test.go deleted file mode 100755 index 8be9dc8..00000000 --- a/dump/dump_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package dump - -import ( - "encoding/json" - "testing" -) - -const jsonString = ` - [ - { - "type": "group", - "value": [ - "Lorem", - "Ipsum", - "dolor", - "sit", - ["A", "m", "e", "t", [[5,23,42]]] - ] - }, - { - "value": "Hello World" - }, - { - "value": "foobar" - } - ] -` - -func Test_Sprint_JSON(t *testing.T) { - var obj interface{} - json.Unmarshal([]byte(jsonString), &obj) - t.Logf("%#v \n%#v", obj, &obj) - if Sprint(obj) != `[ - { - "type" => "group" - "value" => [ - 0 => "Lorem" - 1 => "Ipsum" - 2 => "dolor" - 3 => "sit" - 4 => [ - 0 => "A" - 1 => "m" - 2 => "e" - 3 => "t" - 4 => [ - 0 => [ - 0 => 5 - 1 => 23 - 2 => 42 - ] - ] - ] - ] - } - { - "value" => "Hello World" - } - { - "value" => "foobar" - } -] -` { - t.Fatalf("Dump_Sprint JSON Test failed: %v", Sprint(obj)) - } -} diff --git a/dump/example_test.go b/dump/example_test.go deleted file mode 100755 index fb0cca6..00000000 --- a/dump/example_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package dump_test - -import ( - "encoding/json" - "simonwaldherr.de/go/golibs/dump" -) - -func ExampleSprint_array() { - var obj interface{} - json.Unmarshal([]byte("[[[1,2,3]]]"), &obj) - dump.Print(obj) - - // Output: - // [ - // 0 => [ - // 0 => [ - // 0 => 1 - // 1 => 2 - // 2 => 3 - // ] - // ] - // ] -} - -/* -func ExampleSprint_object() { - var obj interface{} - json.Unmarshal([]byte(`{"a": {"b": {"c": "d"}, "e": "f"}}`), &obj) - dump.Print(obj) - - // Output: - // { - // "a" => { - // "b" => { - // "c" => "d" - // } - // } - // "e" => "f" - // } -} -*/ \ No newline at end of file diff --git a/node/node.go b/node/node.go index c01e052..0f28d4f 100755 --- a/node/node.go +++ b/node/node.go @@ -1,25 +1,25 @@ package node func Node(in *interface{}, handler func(*string, *int, *interface{}, int)) { - nodeHelper(in, handler, 0) + nodeHelper(in, handler, 0) } func nodeHelper(node *interface{}, handler func(*string, *int, *interface{}, int), depth int) { - if node == nil { - return - } - o, isObject := (*node).(map[string]interface{}) - if isObject { - for k, v := range o { - handler(&k, nil, &v, depth) - nodeHelper(&v, handler, depth+1) - } - } - a, isArray := (*node).([]interface{}) - if isArray { - for i, x := range a { - handler(nil, &i, &x, depth) - nodeHelper(&x, handler, depth+1) - } - } + if node == nil { + return + } + o, isObject := (*node).(map[string]interface{}) + if isObject { + for k, v := range o { + handler(&k, nil, &v, depth) + nodeHelper(&v, handler, depth+1) + } + } + a, isArray := (*node).([]interface{}) + if isArray { + for i, x := range a { + handler(nil, &i, &x, depth) + nodeHelper(&x, handler, depth+1) + } + } }