Skip to content
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

Merged
merged 1 commit into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Mark Bates
Copyright (c) 2019 Mark Bates

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
46 changes: 26 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
TAGS ?= "sqlite"
GO_BIN ?= go
TAGS ?= ""
GO_BIN ?= "go"

install:
packr
$(GO_BIN) install -v .
install:
$(GO_BIN) install -tags ${TAGS} -v .
make tidy

tidy:
Expand All @@ -13,43 +12,50 @@ else
echo skipping go mod tidy
endif


deps:
$(GO_BIN) get github.com/gobuffalo/release
$(GO_BIN) get github.com/gobuffalo/packr/packr
$(GO_BIN) get -tags ${TAGS} -t ./...
make tidy

build:
packr
build:
$(GO_BIN) build -v .
make tidy

test:
packr
$(GO_BIN) test -tags ${TAGS} ./...
test:
$(GO_BIN) test -cover -tags ${TAGS} ./...
make tidy

ci-test:
ci-deps:
$(GO_BIN) get -tags ${TAGS} -t ./...

ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy

lint:
gometalinter --vendor ./... --deadline=1m --skip=internal
go get github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run --enable-all
make tidy

update:
ifeq ($(GO111MODULE),on)
rm go.*
$(GO_BIN) mod init
$(GO_BIN) mod tidy
else
$(GO_BIN) get -u -tags ${TAGS}
make tidy
packr
endif
make test
make install
make tidy

release-test:
release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy

release:
release -y -f version.go
$(GO_BIN) get github.com/gobuffalo/release
make tidy
release -y -f version.go --skip-packr
make tidy



4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package events

import (
"encoding/json"
"errors"
"fmt"
"strings"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ func (e Event) MarshalJSON() ([]byte, error) {
// Validate that an event is ready to be emitted
func (e Event) Validate() error {
if len(e.Kind) == 0 {
return errors.New("kind can not be blank")
return fmt.Errorf("kind can not be blank")
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"fmt"
"testing"

"github.com/gobuffalo/mapi"
"github.com/pkg/errors"
"github.com/gobuffalo/events/internal/mapi"
"github.com/stretchr/testify/require"
)

Expand All @@ -33,7 +32,7 @@ func Test_Event_MarshalJSON(t *testing.T) {
Payload: mapi.Mapi{
"p": "P",
},
Error: errors.New("E"),
Error: fmt.Errorf("E"),
},
out: map[string]interface{}{
"kind": "K",
Expand Down
2 changes: 1 addition & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package events
import (
"strings"

"github.com/gobuffalo/mapi"
"github.com/gobuffalo/events/internal/mapi"
)

type Payload = mapi.Mapi
Expand Down
4 changes: 2 additions & 2 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package events

import (
"context"
"fmt"
"sync"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -91,7 +91,7 @@ func Test_EmitError(t *testing.T) {
})

for i := 0; i < max; i++ {
err := EmitError("foo", errors.New("bar"), i)
err := EmitError("foo", fmt.Errorf("bar"), i)
r.NoError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package events
import (
"regexp"

"github.com/markbates/safe"
"github.com/gobuffalo/events/internal/safe"
)

// Filter compiles the string as a regex and returns
Expand Down
9 changes: 3 additions & 6 deletions go.mod
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
11 changes: 1 addition & 10 deletions go.sum
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=
108 changes: 108 additions & 0 deletions internal/mapi/mapi.go
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")
Copy link

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

Suggested change
var ErrNotFound = fmt.Errorf("not found")
// ErrNotFound ...
var ErrNotFound = fmt.Errorf("not found")


type Mapi map[string]interface{}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "Mapi" should have comment

Suggested change
type Mapi map[string]interface{}
// Mapi ...
type Mapi map[string]interface{}


func (mi Mapi) String() string {
b, _ := mi.MarshalJSON()
return string(b)
}

func (mi Mapi) Interface() interface{} {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "Interface" should have comment

Suggested change
func (mi Mapi) Interface() interface{} {
// Interface ...
func (mi Mapi) Interface() interface{} {

return mi
}

func (mi Mapi) Pluck(s string) (interface{}, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "Pluck" should have comment

Suggested change
func (mi Mapi) Pluck(s string) (interface{}, error) {
// Pluck ...
func (mi Mapi) Pluck(s string) (interface{}, error) {

keys := strings.Split(s, ":")
return reduce(keys, mi)
}

func (mi *Mapi) UnmarshalJSON(b []byte) error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "UnmarshalJSON" should have comment

Suggested change
func (mi *Mapi) UnmarshalJSON(b []byte) error {
// UnmarshalJSON ...
func (mi *Mapi) UnmarshalJSON(b []byte) error {

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "MarshalJSON" should have comment

Suggested change
func (mi Mapi) MarshalJSON() ([]byte, error) {
// MarshalJSON ...
func (mi Mapi) MarshalJSON() ([]byte, error) {

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())
}
Loading