Unverified Commit 25326337 authored by Hanzei's avatar Hanzei Committed by GitHub
Browse files

Fix indentation (#69)

* Fix indentation

* Fix ObjxMapSlice()

* Fix more indentations

* Remove golint

* Simplfy lint command
parent 6667b535
......@@ -20,7 +20,6 @@ install:
- mv task $HOME/gopath/bin
script:
- task dl-deps
- task lint
- task test-coverage
......
default:
deps: [test]
dl-deps:
desc: Downloads cli dependencies
cmds:
- go get -u github.com/golang/lint/golint
update-deps:
desc: Updates dependencies
cmds:
......@@ -15,9 +10,8 @@ update-deps:
lint:
desc: Runs golint
cmds:
- go fmt $(go list ./... | grep -v /vendor/)
- go vet $(go list ./... | grep -v /vendor/)
- golint $(ls *.go | grep -v "doc.go")
- gofmt -d -s *.go
- go vet .
silent: true
test:
......
/*
{4} ({1} and []{1})
{4} ({1} and []{1})
*/
// {4} gets the value as a {1}, returns the optionalDefault
// value or a system default object if the value is the wrong type.
func (v *Value) {4}(optionalDefault ...{1}) {1} {
if s, ok := v.data.({1}); ok {
return s
}
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
return {3}
if s, ok := v.data.({1}); ok {
return s
}
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
return {3}
}
// Must{4} gets the value as a {1}.
//
// Panics if the object is not a {1}.
func (v *Value) Must{4}() {1} {
return v.data.({1})
return v.data.({1})
}
// {4}Slice gets the value as a []{1}, returns the optionalDefault
// value or nil if the value is not a []{1}.
func (v *Value) {4}Slice(optionalDefault ...[]{1}) []{1} {
if s, ok := v.data.([]{1}); ok {
return s
}
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
return nil
if s, ok := v.data.([]{1}); ok {
return s
}
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
return nil
}
// Must{4}Slice gets the value as a []{1}.
//
// Panics if the object is not a []{1}.
func (v *Value) Must{4}Slice() []{1} {
return v.data.([]{1})
return v.data.([]{1})
}
// Is{4} gets whether the object contained is a {1} or not.
func (v *Value) Is{4}() bool {
_, ok := v.data.({1})
return ok
_, ok := v.data.({1})
return ok
}
// Is{4}Slice gets whether the object contained is a []{1} or not.
func (v *Value) Is{4}Slice() bool {
_, ok := v.data.([]{1})
return ok
_, ok := v.data.([]{1})
return ok
}
// Each{4} calls the specified callback for each object
......@@ -57,68 +57,68 @@ func (v *Value) Is{4}Slice() bool {
//
// Panics if the object is the wrong type.
func (v *Value) Each{4}(callback func(int, {1}) bool) *Value {
for index, val := range v.Must{4}Slice() {
carryon := callback(index, val)
if !carryon {
break
}
}
return v
for index, val := range v.Must{4}Slice() {
carryon := callback(index, val)
if !carryon {
break
}
}
return v
}
// Where{4} uses the specified decider function to select items
// from the []{1}. The object contained in the result will contain
// only the selected items.
func (v *Value) Where{4}(decider func(int, {1}) bool) *Value {
var selected []{1}
v.Each{4}(func(index int, val {1}) bool {
shouldSelect := decider(index, val)
if !shouldSelect {
selected = append(selected, val)
}
return true
})
return &Value{data:selected}
var selected []{1}
v.Each{4}(func(index int, val {1}) bool {
shouldSelect := decider(index, val)
if !shouldSelect {
selected = append(selected, val)
}
return true
})
return &Value{data:selected}
}
// Group{4} uses the specified grouper function to group the items
// keyed by the return of the grouper. The object contained in the
// result will contain a map[string][]{1}.
func (v *Value) Group{4}(grouper func(int, {1}) string) *Value {
groups := make(map[string][]{1})
v.Each{4}(func(index int, val {1}) bool {
group := grouper(index, val)
if _, ok := groups[group]; !ok {
groups[group] = make([]{1}, 0)
}
groups[group] = append(groups[group], val)
return true
})
return &Value{data:groups}
groups := make(map[string][]{1})
v.Each{4}(func(index int, val {1}) bool {
group := grouper(index, val)
if _, ok := groups[group]; !ok {
groups[group] = make([]{1}, 0)
}
groups[group] = append(groups[group], val)
return true
})
return &Value{data:groups}
}
// Replace{4} uses the specified function to replace each {1}s
// by iterating each item. The data in the returned result will be a
// []{1} containing the replaced items.
func (v *Value) Replace{4}(replacer func(int, {1}) {1}) *Value {
arr := v.Must{4}Slice()
replaced := make([]{1}, len(arr))
v.Each{4}(func(index int, val {1}) bool {
replaced[index] = replacer(index, val)
return true
})
return &Value{data:replaced}
arr := v.Must{4}Slice()
replaced := make([]{1}, len(arr))
v.Each{4}(func(index int, val {1}) bool {
replaced[index] = replacer(index, val)
return true
})
return &Value{data:replaced}
}
// Collect{4} uses the specified collector function to collect a value
// for each of the {1}s in the slice. The data returned will be a
// []interface{}.
func (v *Value) Collect{4}(collector func(int, {1}) interface{}) *Value {
arr := v.Must{4}Slice()
collected := make([]interface{}, len(arr))
v.Each{4}(func(index int, val {1}) bool {
collected[index] = collector(index, val)
return true
})
return &Value{data:collected}
arr := v.Must{4}Slice()
collected := make([]interface{}, len(arr))
v.Each{4}(func(index int, val {1}) bool {
collected[index] = collector(index, val)
return true
})
return &Value{data:collected}
}
/*
Tests for {4} ({1} and []{1})
Tests for {4} ({1} and []{1})
*/
func Test{4}(t *testing.T) {
val := {1}({2})
m := objx.Map{"value": val, "nothing": nil}
assert.Equal(t, val, m.Get("value").{4}())
assert.Equal(t, val, m.Get("value").Must{4}())
assert.Equal(t, {1}({3}), m.Get("nothing").{4}())
assert.Equal(t, val, m.Get("nothing").{4}({2}))
assert.Panics(t, func() {
m.Get("age").Must{4}()
})
val := {1}({2})
m := objx.Map{"value": val, "nothing": nil}
assert.Equal(t, val, m.Get("value").{4}())
assert.Equal(t, val, m.Get("value").Must{4}())
assert.Equal(t, {1}({3}), m.Get("nothing").{4}())
assert.Equal(t, val, m.Get("nothing").{4}({2}))
assert.Panics(t, func() {
m.Get("age").Must{4}()
})
}
func Test{4}Slice(t *testing.T) {
val := {1}({2})
m := objx.Map{"value": []{1}{ val }, "nothing": nil}
assert.Equal(t, val, m.Get("value").{4}Slice()[0])
assert.Equal(t, val, m.Get("value").Must{4}Slice()[0])
assert.Equal(t, []{1}(nil), m.Get("nothing").{4}Slice())
assert.Equal(t, val, m.Get("nothing").{4}Slice([]{1}{{1}({2})})[0])
assert.Panics(t, func() {
m.Get("nothing").Must{4}Slice()
})
val := {1}({2})
m := objx.Map{"value": []{1}{ val }, "nothing": nil}
assert.Equal(t, val, m.Get("value").{4}Slice()[0])
assert.Equal(t, val, m.Get("value").Must{4}Slice()[0])
assert.Equal(t, []{1}(nil), m.Get("nothing").{4}Slice())
assert.Equal(t, val, m.Get("nothing").{4}Slice([]{1}{{1}({2})})[0])
assert.Panics(t, func() {
m.Get("nothing").Must{4}Slice()
})
}
func TestIs{4}(t *testing.T) {
m := objx.Map{"data": {1}({2})}
m := objx.Map{"data": {1}({2})}
assert.True(t, m.Get("data").Is{4}())
assert.True(t, m.Get("data").Is{4}())
}
func TestIs{4}Slice(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2})}}
m := objx.Map{"data": []{1}{{1}({2})}}
assert.True(t, m.Get("data").Is{4}Slice())
assert.True(t, m.Get("data").Is{4}Slice())
}
func TestEach{4}(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
count := 0
replacedVals := make([]{1}, 0)
assert.Equal(t, m.Get("data"), m.Get("data").Each{4}(func(i int, val {1}) bool {
count++
replacedVals = append(replacedVals, val)
// abort early
return i != 2
}))
assert.Equal(t, count, 3)
assert.Equal(t, replacedVals[0], m.Get("data").Must{4}Slice()[0])
assert.Equal(t, replacedVals[1], m.Get("data").Must{4}Slice()[1])
assert.Equal(t, replacedVals[2], m.Get("data").Must{4}Slice()[2])
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
count := 0
replacedVals := make([]{1}, 0)
assert.Equal(t, m.Get("data"), m.Get("data").Each{4}(func(i int, val {1}) bool {
count++
replacedVals = append(replacedVals, val)
// abort early
return i != 2
}))
assert.Equal(t, count, 3)
assert.Equal(t, replacedVals[0], m.Get("data").Must{4}Slice()[0])
assert.Equal(t, replacedVals[1], m.Get("data").Must{4}Slice()[1])
assert.Equal(t, replacedVals[2], m.Get("data").Must{4}Slice()[2])
}
func TestWhere{4}(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
selected := m.Get("data").Where{4}(func(i int, val {1}) bool {
return i%2 == 0
}).Must{4}Slice()
selected := m.Get("data").Where{4}(func(i int, val {1}) bool {
return i%2 == 0
}).Must{4}Slice()
assert.Equal(t, 3, len(selected))
assert.Equal(t, 3, len(selected))
}
func TestGroup{4}(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
grouped := m.Get("data").Group{4}(func(i int, val {1}) string {
return fmt.Sprintf("%v", i%2==0)
}).Data().(map[string][]{1})
grouped := m.Get("data").Group{4}(func(i int, val {1}) string {
return fmt.Sprintf("%v", i%2==0)
}).Data().(map[string][]{1})
assert.Equal(t, 2, len(grouped))
assert.Equal(t, 3, len(grouped["true"]))
assert.Equal(t, 3, len(grouped["false"]))
assert.Equal(t, 2, len(grouped))
assert.Equal(t, 3, len(grouped["true"]))
assert.Equal(t, 3, len(grouped["false"]))
}
func TestReplace{4}(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
rawArr := m.Get("data").Must{4}Slice()
replaced := m.Get("data").Replace{4}(func(index int, val {1}) {1} {
if index < len(rawArr)-1 {
return rawArr[index+1]
}
return rawArr[0]
})
replacedArr := replaced.Must{4}Slice()
if assert.Equal(t, 6, len(replacedArr)) {
assert.Equal(t, replacedArr[0], rawArr[1])
assert.Equal(t, replacedArr[1], rawArr[2])
assert.Equal(t, replacedArr[2], rawArr[3])
assert.Equal(t, replacedArr[3], rawArr[4])
assert.Equal(t, replacedArr[4], rawArr[5])
assert.Equal(t, replacedArr[5], rawArr[0])
}
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
rawArr := m.Get("data").Must{4}Slice()
replaced := m.Get("data").Replace{4}(func(index int, val {1}) {1} {
if index < len(rawArr)-1 {
return rawArr[index+1]
}
return rawArr[0]
})
replacedArr := replaced.Must{4}Slice()
if assert.Equal(t, 6, len(replacedArr)) {
assert.Equal(t, replacedArr[0], rawArr[1])
assert.Equal(t, replacedArr[1], rawArr[2])
assert.Equal(t, replacedArr[2], rawArr[3])
assert.Equal(t, replacedArr[3], rawArr[4])
assert.Equal(t, replacedArr[4], rawArr[5])
assert.Equal(t, replacedArr[5], rawArr[0])
}
}
func TestCollect{4}(t *testing.T) {
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
collected := m.Get("data").Collect{4}(func(index int, val {1}) interface{} {
return index
})
collectedArr := collected.MustInterSlice()
if assert.Equal(t, 6, len(collectedArr)) {
assert.Equal(t, collectedArr[0], 0)
assert.Equal(t, collectedArr[1], 1)
assert.Equal(t, collectedArr[2], 2)
assert.Equal(t, collectedArr[3], 3)
assert.Equal(t, collectedArr[4], 4)
assert.Equal(t, collectedArr[5], 5)
}
m := objx.Map{"data": []{1}{{1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2})}}
collected := m.Get("data").Collect{4}(func(index int, val {1}) interface{} {
return index
})
collectedArr := collected.MustInterSlice()
if assert.Equal(t, 6, len(collectedArr)) {
assert.Equal(t, collectedArr[0], 0)
assert.Equal(t, collectedArr[1], 1)
assert.Equal(t, collectedArr[2], 2)
assert.Equal(t, collectedArr[3], 3)
assert.Equal(t, collectedArr[4], 4)
assert.Equal(t, collectedArr[5], 5)
}
}
......@@ -128,7 +128,7 @@ func TestJSONSliceInt(t *testing.T) {
assert.Nil(t, err)
require.NotNil(t, m)
assert.Equal(t, []objx.Map{objx.Map{"b": 1}, objx.Map{"c": 2}}, m.Get("a").ObjxMapSlice())
assert.Equal(t, []objx.Map{{"b": 1}, {"c": 2}}, m.Get("a").ObjxMapSlice())
}
func TestJSONSliceMixed(t *testing.T) {
......
package objx
/*
Inter (interface{} and []interface{})
Inter (interface{} and []interface{})
*/
// Inter gets the value as a interface{}, returns the optionalDefault
......@@ -126,7 +126,7 @@ func (v *Value) CollectInter(collector func(int, interface{}) interface{}) *Valu
}
/*
MSI (map[string]interface{} and []map[string]interface{})
MSI (map[string]interface{} and []map[string]interface{})
*/
// MSI gets the value as a map[string]interface{}, returns the optionalDefault
......@@ -251,7 +251,7 @@ func (v *Value) CollectMSI(collector func(int, map[string]interface{}) interface
}
/*
ObjxMap ((Map) and [](Map))
ObjxMap ((Map) and [](Map))
*/
// ObjxMap gets the value as a (Map), returns the optionalDefault
......@@ -276,6 +276,7 @@ func (v *Value) MustObjxMap() Map {
// ObjxMapSlice gets the value as a [](Map), returns the optionalDefault
// value or nil if the value is not a [](Map).
func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
// TODO: Find a better solution for the Map type as this code is autogenerated
if s, ok := v.data.([]Map); ok {
return s
}
......@@ -283,9 +284,8 @@ func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
if !ok {
if len(optionalDefault) == 1 {
return optionalDefault[0]
} else {
return nil
}
return nil
}
result := make([]Map, len(s))
......@@ -391,7 +391,7 @@ func (v *Value) CollectObjxMap(collector func(int, Map) interface{}) *Value {
}
/*
Bool (bool and []bool)
Bool (bool and []bool)
*/
// Bool gets the value as a bool, returns the optionalDefault
......@@ -516,7 +516,7 @@ func (v *Value) CollectBool(collector func(int, bool) interface{}) *Value {
}
/*
Str (string and []string)
Str (string and []string)
*/
// Str gets the value as a string, returns the optionalDefault
......@@ -641,7 +641,7 @@ func (v *Value) CollectStr(collector func(int, string) interface{}) *Value {
}
/*
Int (int and []int)
Int (int and []int)
*/
// Int gets the value as a int, returns the optionalDefault
......@@ -766,7 +766,7 @@ func (v *Value) CollectInt(collector func(int, int) interface{}) *Value {
}
/*
Int8 (int8 and []int8)
Int8 (int8 and []int8)
*/
// Int8 gets the value as a int8, returns the optionalDefault
......@@ -891,7 +891,7 @@ func (v *Value) CollectInt8(collector func(int, int8) interface{}) *Value {
}
/*
Int16 (int16 and []int16)
Int16 (int16 and []int16)
*/
// Int16 gets the value as a int16, returns the optionalDefault
......@@ -1016,7 +1016,7 @@ func (v *Value) CollectInt16(collector func(int, int16) interface{}) *Value {
}
/*
Int32 (int32 and []int32)
Int32 (int32 and []int32)
*/
// Int32 gets the value as a int32, returns the optionalDefault
......@@ -1141,7 +1141,7 @@ func (v *Value) CollectInt32(collector func(int, int32) interface{}) *Value {
}
/*
Int64 (int64 and []int64)
Int64 (int64 and []int64)
*/
// Int64 gets the value as a int64, returns the optionalDefault
......@@ -1266,7 +1266,7 @@ func (v *Value) CollectInt64(collector func(int, int64) interface{}) *Value {
}
/*
Uint (uint and []uint)
Uint (uint and []uint)
*/
// Uint gets the value as a uint, returns the optionalDefault
......@@ -1391,7 +1391,7 @@ func (v *Value) CollectUint(collector func(int, uint) interface{}) *Value {
}
/*
Uint8 (uint8 and []uint8)
Uint8 (uint8 and []uint8)
*/
// Uint8 gets the value as a uint8, returns the optionalDefault
......@@ -1516,7 +1516,7 @@ func (v *Value) CollectUint8(collector func(int, uint8) interface{}) *Value {
}
/*
Uint16 (uint16 and []uint16)
Uint16 (uint16 and []uint16)
*/
// Uint16 gets the value as a uint16, returns the optionalDefault
......@@ -1641,7 +1641,7 @@ func (v *Value) CollectUint16(collector func(int, uint16) interface{}) *Value {
}
/*
Uint32 (uint32 and []uint32)
Uint32 (uint32 and []uint32)
*/
// Uint32 gets the value as a uint32, returns the optionalDefault
......@@ -1766,7 +1766,7 @@ func (v *Value) CollectUint32(collector func(int, uint32) interface{}) *Value {
}
/*
Uint64 (uint64 and []uint64)
Uint64 (uint64 and []uint64)
*/
// Uint64 gets the value as a uint64, returns the optionalDefault
......@@ -1891,7 +1891,7 @@ func (v *Value) CollectUint64(collector func(int, uint64) interface{}) *Value {
}
/*
Uintptr (uintptr and []uintptr)
Uintptr (uintptr and []uintptr)
*/
// Uintptr gets the value as a uintptr, returns the optionalDefault
......@@ -2016,7 +2016,7 @@ func (v *Value) CollectUintptr(collector func(int, uintptr) interface{}) *Value
}
/*
Float32 (float32 and []float32)
Float32 (float32 and []float32)
*/
// Float32 gets the value as a float32, returns the optionalDefault
......@@ -2141,7 +2141,7 @@ func (v *Value) CollectFloat32(collector func(int, float32) interface{}) *Value
}
/*
Float64 (float64 and []float64)
Float64 (float64 and []float64)
*/
// Float64 gets the value as a float64, returns the optionalDefault
......@@ -2266,7 +2266,7 @@ func (v *Value) CollectFloat64(collector func(int, float64) interface{}) *Value
}
/*
Complex64 (complex64 and []complex64)
Complex64 (complex64 and []complex64)
*/
// Complex64 gets the value as a complex64, returns the optionalDefault
......@@ -2391,7 +2391,7 @@ func (v *Value) CollectComplex64(collector func(int, complex64) interface{}) *Va
}
/*
Complex128 (complex128 and []complex128)
Complex128 (complex128 and []complex128)
*/
// Complex128 gets the value as a complex128, returns the optionalDefault
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment