-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
went on a bit of a dep diet. 😃 #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
module github.com/gobuffalo/events | ||
|
||
require ( | ||
github.com/gobuffalo/mapi v1.0.1 | ||
github.com/markbates/safe v1.0.1 | ||
github.com/pkg/errors v0.8.1 | ||
github.com/stretchr/testify v1.3.0 | ||
) | ||
go 1.12 | ||
|
||
require github.com/stretchr/testify v1.3.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gobuffalo/mapi v1.0.1 h1:JRuTiZzDEZhBHkFiHTxJkYRT6CbYuL0K/rn+1byJoEA= | ||
github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= | ||
github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= | ||
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= | ||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||||
package mapi | ||||||||
|
||||||||
import ( | ||||||||
"encoding/json" | ||||||||
"fmt" | ||||||||
"reflect" | ||||||||
"strings" | ||||||||
) | ||||||||
|
||||||||
var ErrNotFound = fmt.Errorf("not found") | ||||||||
|
||||||||
type Mapi map[string]interface{} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported identifier "Mapi" should have comment
Suggested change
|
||||||||
|
||||||||
func (mi Mapi) String() string { | ||||||||
b, _ := mi.MarshalJSON() | ||||||||
return string(b) | ||||||||
} | ||||||||
|
||||||||
func (mi Mapi) Interface() interface{} { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported identifier "Interface" should have comment
Suggested change
|
||||||||
return mi | ||||||||
} | ||||||||
|
||||||||
func (mi Mapi) Pluck(s string) (interface{}, error) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported identifier "Pluck" should have comment
Suggested change
|
||||||||
keys := strings.Split(s, ":") | ||||||||
return reduce(keys, mi) | ||||||||
} | ||||||||
|
||||||||
func (mi *Mapi) UnmarshalJSON(b []byte) error { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported identifier "UnmarshalJSON" should have comment
Suggested change
|
||||||||
mm := map[string]interface{}{} | ||||||||
if err := json.Unmarshal(b, &mm); err != nil { | ||||||||
return err | ||||||||
} | ||||||||
unmarshal(mm) | ||||||||
(*mi) = Mapi(mm) | ||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
func unmarshal(m map[string]interface{}) { | ||||||||
for k, v := range m { | ||||||||
if mv, ok := v.(map[string]interface{}); ok { | ||||||||
unmarshal(mv) | ||||||||
m[k] = Mapi(mv) | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (mi Mapi) MarshalJSON() ([]byte, error) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported identifier "MarshalJSON" should have comment
Suggested change
|
||||||||
m := map[string]interface{}{} | ||||||||
|
||||||||
for k, v := range mi { | ||||||||
rv := reflect.Indirect(reflect.ValueOf(v)) | ||||||||
switch rv.Kind() { | ||||||||
case reflect.Map: | ||||||||
mm := Mapi{} | ||||||||
for _, xk := range rv.MapKeys() { | ||||||||
mm[xk.String()] = rv.MapIndex(xk).Interface() | ||||||||
} | ||||||||
m[k] = mm | ||||||||
default: | ||||||||
if _, ok := v.(Mapi); ok { | ||||||||
continue | ||||||||
} | ||||||||
if _, err := json.Marshal(v); err == nil { | ||||||||
// if it can be marshaled, add it to the map | ||||||||
m[k] = v | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
return json.Marshal(m) | ||||||||
} | ||||||||
|
||||||||
func reduce(keys []string, in interface{}) (interface{}, error) { | ||||||||
if len(keys) == 0 { | ||||||||
return nil, ErrNotFound | ||||||||
} | ||||||||
|
||||||||
rv := reflect.Indirect(reflect.ValueOf(in)) | ||||||||
if !rv.IsValid() { | ||||||||
return nil, ErrNotFound | ||||||||
} | ||||||||
if rv.Kind() != reflect.Map { | ||||||||
return nil, ErrNotFound | ||||||||
} | ||||||||
|
||||||||
var key reflect.Value | ||||||||
for _, k := range rv.MapKeys() { | ||||||||
if k.String() == keys[0] { | ||||||||
key = k | ||||||||
break | ||||||||
} | ||||||||
} | ||||||||
if !key.IsValid() { | ||||||||
return nil, ErrNotFound | ||||||||
} | ||||||||
|
||||||||
keys = keys[1:] | ||||||||
iv := rv.MapIndex(key) | ||||||||
|
||||||||
if !iv.IsValid() { | ||||||||
return nil, ErrNotFound | ||||||||
} | ||||||||
|
||||||||
if len(keys) == 0 { | ||||||||
return iv.Interface(), nil | ||||||||
} | ||||||||
|
||||||||
return reduce(keys, iv.Interface()) | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported identifier "ErrNotFound" should have comment