Skip to content

Commit

Permalink
Merge pull request #15 from linkpoolio/feature/single-val
Browse files Browse the repository at this point in the history
Removal of uneeded merge of result with resp for single values
  • Loading branch information
jleeh authored Aug 25, 2019
2 parents eb0eba6 + f550bfd commit fdae295
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 60 deletions.
34 changes: 0 additions & 34 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,6 @@ func (j *JSON) MarshalJSON() ([]byte, error) {
return []byte("{}"), nil
}

// Merge combines the given JSON with the existing JSON.
func (j *JSON) Merge(j2 *JSON) (*JSON, error) {
if j2 == nil || j == nil {
return j, nil
}

if j.Type != gjson.JSON && j.Type != gjson.Null {
return nil, errors.New("Cannot merge response and request")
}

body := j.Map()
for key, value := range j2.Map() {
body[key] = value
}

cleaned := map[string]interface{}{}
for k, v := range body {
cleaned[k] = v.Value()
}

b, err := json.Marshal(cleaned)
if err != nil {
return nil, err
}

var rval *JSON
return rval, gjson.Unmarshal(b, &rval)
}

// SetCompleted marks a result as errored
func (r *Result) SetErrored(err error) {
r.Status = "errored"
Expand Down Expand Up @@ -239,9 +210,6 @@ func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
} else if data, err := ParseInterface(obj); err != nil {
cc <- http.StatusInternalServerError
rt.SetErrored(err)
} else if data, err := data.Merge(rt.Data); err != nil {
cc <- http.StatusInternalServerError
rt.SetErrored(err)
} else {
rt.Data = data
rt.SetCompleted()
Expand All @@ -254,8 +222,6 @@ func (s *Server) Lambda(r *Result) (interface{}, error) {
r.SetErrored(err)
} else if data, err := ParseInterface(obj); err != nil {
r.SetErrored(err)
} else if data, err := data.Merge(r.Data); err != nil {
r.SetErrored(err)
} else {
r.SetCompleted()
r.Data = data
Expand Down
17 changes: 0 additions & 17 deletions bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,6 @@ import (
"testing"
)

func TestJSON_Merge(t *testing.T) {
j1, err := ParseInterface(map[string]string{
"alice": "bob",
"carl": "dennis",
})
assert.Nil(t, err)
j2, err := ParseInterface(map[string]string{
"alice": "bob",
"eric": "fred",
"geoff": "harry",
})
j3, err := j1.Merge(j2)
assert.Nil(t, err)
assert.Equal(t, "bob", j3.Get("alice").String())
assert.Equal(t, "fred", j3.Get("eric").String())
}

// Copied from the example to use as a test fixture
type CryptoCompare struct{}

Expand Down
31 changes: 31 additions & 0 deletions examples/gasstation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/linkpoolio/bridges/bridge"
"net/http"
)

type GasStation struct{}

// Run implements Bridge Run for querying the Wolfram short answers API
func (gs *GasStation) Run(h *bridge.Helper) (interface{}, error) {
obj := make(map[string]interface{})
err := h.HTTPCall(
http.MethodGet,
"https://ethgasstation.info/json/ethgasAPI.json",
&obj,
)
return obj["average"], err
}

// Opts is the bridge.Bridge implementation
func (gs *GasStation) Opts() *bridge.Opts {
return &bridge.Opts{
Name: "GasStation",
Lambda: true,
}
}

func main() {
bridge.NewServer(&GasStation{}).Start(8080)
}
26 changes: 26 additions & 0 deletions examples/gasstation/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"github.com/linkpoolio/bridges/bridge"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGasStation_Run(t *testing.T) {
wa := GasStation{}

h := bridge.NewHelper(&bridge.JSON{})
val, err := wa.Run(h)

avg, ok := val.(float64)
assert.True(t, ok)
assert.True(t, avg > 0)
assert.Nil(t, err)
}

func TestGasStation_Opts(t *testing.T) {
cc := GasStation{}
opts := cc.Opts()
assert.Equal(t, opts.Name, "GasStation")
assert.True(t, opts.Lambda)
}
3 changes: 1 addition & 2 deletions go.mod
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.12

require (
github.com/aws/aws-lambda-go v1.10.0
github.com/haya14busa/goverage v0.0.0-20180129164344-eec3514a20b5 // indirect
github.com/montanaflynn/stats v0.5.0
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/sirupsen/logrus v1.4.1
Expand All @@ -14,6 +13,6 @@ require (
github.com/tidwall/match v1.0.1 // indirect
github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65 // indirect
github.com/utahta/go-openuri v0.1.0
golang.org/x/tools v0.0.0-20190415154727-2b5498619ef1 // indirect
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a // indirect
gopkg.in/guregu/null.v3 v3.4.0
)
7 changes: 0 additions & 7 deletions go.sum
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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/haya14busa/goverage v0.0.0-20180129164344-eec3514a20b5 h1:FdBGmSkD2QpQzRWup//SGObvWf2nq89zj9+ta9OvI3A=
github.com/haya14busa/goverage v0.0.0-20180129164344-eec3514a20b5/go.mod h1:0YZ2wQSuwviXXXGUiK6zXzskyBLAbLXhamxzcFHSLoM=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/montanaflynn/stats v0.5.0 h1:2EkzeTSqBB4V4bJwWrt5gIIrZmpJBcoIRGS2kWLgzmk=
Expand All @@ -31,14 +29,9 @@ github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65 h1:rQ229MBgvW68s1/g
github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/utahta/go-openuri v0.1.0 h1:5t+GPGBjwEt7oRTMArC47QMIiXarzNO6W3PDy8nXGoM=
github.com/utahta/go-openuri v0.1.0/go.mod h1:wg5kOkfgUWHGFacyyH+FsrEzB++WPpFyKrwDSeo69Ng=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190415154727-2b5498619ef1 h1:NqNgquevQtG0pNgs2516OYQcEEWApBArZGrkOzfOfiY=
golang.org/x/tools v0.0.0-20190415154727-2b5498619ef1/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/guregu/null.v3 v3.4.0 h1:AOpMtZ85uElRhQjEDsFx21BkXqFPwA7uoJukd4KErIs=
gopkg.in/guregu/null.v3 v3.4.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y=

0 comments on commit fdae295

Please sign in to comment.