Skip to content

Commit

Permalink
🎨 add comments on methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PMoneda committed Jun 3, 2017
1 parent ddadec5 commit 134026d
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 44 deletions.
4 changes: 4 additions & 0 deletions choice.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flow

//Choice is the type for conditional structure of a Pipe
type Choice struct {
pipe IPipe
execute bool
Expand All @@ -16,6 +17,7 @@ func NewChoice(p IPipe) *Choice {
return &c
}

//To execute a connector
func (c *Choice) To(url string, params ...interface{}) *Choice {
if len(c.pipe.GetFails()) > 0 {
return c
Expand All @@ -27,6 +29,7 @@ func (c *Choice) To(url string, params ...interface{}) *Choice {
return c
}

// When is the conditional tester
func (c *Choice) When(e HeaderFnc) *Choice {
if len(c.pipe.GetFails()) > 0 {
return c
Expand All @@ -37,6 +40,7 @@ func (c *Choice) When(e HeaderFnc) *Choice {
return c
}

//Otherwise is executed when others condiotions is not true - Like Else
func (c *Choice) Otherwise() *Choice {
if len(c.pipe.GetFails()) > 0 {
return c
Expand Down
34 changes: 26 additions & 8 deletions choice_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package flow

import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestChoice(t *testing.T) {
p := NewPipe().From("direct:a")
p = p.SetBody("Hello").SetHeader("status", "300")
ch := p.Choice()
ch = ch.When(Header("status").IsEqualTo("200")).To("print:https://?msg=200")
ch = ch.When(Header("status").IsEqualTo("400")).To("print:https://?msg=400")
b := ch.Otherwise().Body()
fmt.Println(b)
executeFlow := func(s string) string {
p := NewPipe().From("direct:a")
p = p.SetBody("Hello").SetHeader("status", s)
ch := p.Choice()
ch = ch.When(Header("status").IsEqualTo("200")).To("set:https://?prop=body", "200")
ch = ch.When(Header("status").IsEqualTo("400")).To("set:https://?prop=body", "400")
ch = ch.Otherwise().To("set:https://?prop=body", "500")
b := p.GetBody()
switch t := b.(type) {
case string:
return t
default:
return "-1"

}
}
Convey("Should decide test coditional flows", t, func() {

So(executeFlow("200"), ShouldEqual, "200")
So(executeFlow("400"), ShouldEqual, "400")
So(executeFlow("300"), ShouldEqual, "500")

})

}
2 changes: 1 addition & 1 deletion direct.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package flow

func directConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func directConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {
if len(params) > 0 {
e.SetBody(params[0])
}
Expand Down
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import:
version: ~1.0.0
- package: github.com/beevik/etree
version: ~0.0.0
- package: github.com/smartystreets/goconvey
version: ~1.6.2
6 changes: 3 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

var defaultDialer = &net.Dialer{Timeout: 16 * time.Second, KeepAlive: 16 * time.Second}

var cfg *tls.Config = &tls.Config{
var cfg = &tls.Config{
InsecureSkipVerify: true,
}
var client *http.Client = &http.Client{
var client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: cfg,
Dial: defaultDialer.Dial,
Expand All @@ -42,7 +42,7 @@ func getClient(skip string) *http.Client {
}*/
return client
}
func httpConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func httpConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {

newData := NewExchangeMessage()
var skip string
Expand Down
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

func messageConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func messageConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {

if len(params) < 2 {
return errors.New("Message Letter required")
Expand Down
38 changes: 28 additions & 10 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ type IPipe interface {
GetHeader() HeaderMap
Header() HeaderMap
Transform(Transform, string, Transform, template.FuncMap) IPipe
Flush()
Choice() *Choice
}

// Pipe is the main data structure of Flow Pipe controls all the execution flow
type Pipe struct {
pipes Stack
fails []error
}

// NewPipe creates a new empty Pipe
func NewPipe() IPipe {
p := Pipe{
pipes: make(Stack, 0, 0),
Expand All @@ -46,6 +47,7 @@ func NewPipe() IPipe {
return &p
}

//Choice is the conditional flow of a pipe
func (p *Pipe) Choice() *Choice {
if len(p.fails) > 0 {
printFails(p)
Expand All @@ -60,7 +62,10 @@ func (p *Pipe) Choice() *Choice {
}()
return NewChoice(p)
}

//GetBody get final body result from executed flow
func (p *Pipe) GetBody() interface{} {
in := p.pipes.Pop().(Message)
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
Expand All @@ -70,15 +75,15 @@ func (p *Pipe) GetBody() interface{} {
printFails(p)
return p.fails
}
in := p.pipes.Pop().(Message)

for msg := range in {
m := msg.(*ExchangeMessage)
close(in)
return m.body
}
return nil
}

//GetHeader same as GetBody but instead of return body of message this method returns the message header
func (p *Pipe) GetHeader() HeaderMap {
in := p.pipes.Pop().(Message)
for msg := range in {
Expand All @@ -87,6 +92,8 @@ func (p *Pipe) GetHeader() HeaderMap {
}
return nil
}

//Body returns body but doesn't lock the flow
func (p *Pipe) Body() interface{} {
if len(p.fails) > 0 {
printFails(p)
Expand All @@ -108,6 +115,7 @@ func (p *Pipe) Body() interface{} {
return body
}

//Header return current Header but doesn't lock the flow
func (p *Pipe) Header() HeaderMap {

out := make(Message)
Expand All @@ -126,7 +134,7 @@ func (p *Pipe) Header() HeaderMap {
return h
}

//Processor on message
//Processor execute a user-pass go function in the flow
func (p *Pipe) Processor(proc PipeProcessor) IPipe {

if len(p.fails) > 0 {
Expand Down Expand Up @@ -190,6 +198,7 @@ func (p *Pipe) SetBody(b interface{}) IPipe {
return p
}

//From is the entrypoint of a flow, all flows need to be started with From
func (p *Pipe) From(url string, params ...interface{}) IPipe {
if len(p.fails) > 0 {
printFails(p)
Expand All @@ -211,6 +220,7 @@ func (p *Pipe) From(url string, params ...interface{}) IPipe {
return p
}

//Transform -deprecated
func (p *Pipe) Transform(from Transform, mode string, to Transform, fncs template.FuncMap) IPipe {
if len(p.fails) > 0 {
printFails(p)
Expand Down Expand Up @@ -243,6 +253,7 @@ func (p *Pipe) Transform(from Transform, mode string, to Transform, fncs templat
return p
}

//To is a method to create a flow based on connectors
func (p *Pipe) To(url string, params ...interface{}) IPipe {
if len(p.fails) > 0 {
printFails(p)
Expand Down Expand Up @@ -278,50 +289,57 @@ func printFails(p *Pipe) {
fmt.Println(e)
}
}

//GetFails returns all error occured dURIng execution flow
func (p *Pipe) GetFails() []error {
return p.fails
}
func (p *Pipe) Flush() {
for v := range p.pipes.Top().(Message) {
fmt.Println(v)
}
p.pipes.Clear()
}

//ExchangeMessage is the message exchange inner Pipe
type ExchangeMessage struct {
head HeaderMap
body interface{}
}

//SetHeader add a key-value header to a message
func (e *ExchangeMessage) SetHeader(k, v string) {
e.head.Add(k, v)
}

//GetHeader returns a header value based on key
func (e *ExchangeMessage) GetHeader(k string) string {
return e.head.Get(k)
}

//DelHeader delete header from message
func (e *ExchangeMessage) DelHeader(k string) {
e.head.Del(k)
}

//GetHeaderMap return the map from header
func (e *ExchangeMessage) GetHeaderMap() HeaderMap {
return e.head
}

//ClearHeader removes all entries from header
func (e *ExchangeMessage) ClearHeader() {
keys := e.head.ListKeys()
for _, k := range keys {
e.head.Del(k)
}
}

//SetBody writes the body to message
func (e *ExchangeMessage) SetBody(b interface{}) {
e.body = b
}

//GetBody return body from message
func (e *ExchangeMessage) GetBody() interface{} {
return e.body
}

//NewExchangeMessage creates new empty ExchangeMessage
func NewExchangeMessage() *ExchangeMessage {
e := ExchangeMessage{
head: make(HeaderMap),
Expand Down
2 changes: 1 addition & 1 deletion pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestShouldConvertJSONInputMessageToOutPutUsingTransformOnPipe(t *testing.T)
`

r := NewPipe()
b := r.From("https://services.groupkt.com/state/get/IND/UP").Transform(from, "json", to).Body()
b := r.From("https://services.groupkt.com/state/get/IND/UP").Transform(from, "json", to, nil).Body()
if b != expected {
t.Fail()
}
Expand Down
2 changes: 1 addition & 1 deletion print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func printConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func printConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {
msg := u.options.Get("msg")
//TODO refactor
if msg == "${body}" {
Expand Down
2 changes: 1 addition & 1 deletion set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package flow

func setConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func setConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {
if len(params) > 0 {
if u.options.Get("prop") == "body" {
e.SetBody(params[0])
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"html/template"
)

func templateConector(next func(), m *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func templateConector(next func(), m *ExchangeMessage, out Message, u URI, params ...interface{}) error {
var fncs template.FuncMap
if len(params) > 1 && params[1] != nil {
fncs = params[1].(template.FuncMap)
Expand Down
2 changes: 1 addition & 1 deletion transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/beevik/etree"
)

func transformConector(next func(), m *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func transformConector(next func(), m *ExchangeMessage, out Message, u URI, params ...interface{}) error {
t := Transform(m.body.(string))

var trans string
Expand Down
6 changes: 3 additions & 3 deletions transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var inputFormat Transform = `<book><a>{{age}}</a></book><b>{{grade}}</b>`
var outPutFormat Transform = `{ "a":"{{.age}}","b":"{{.grade}}"}`

func TestShouldTransformData(t *testing.T) {
transformed := data.TransformFromXML(inputFormat, outPutFormat)
transformed, _ := data.TransformFromXML(inputFormat, outPutFormat, nil)
if transformed != `{ "a":"10","b":"3"}` {
t.Fail()
}
Expand All @@ -16,7 +16,7 @@ func TestShouldTransformData(t *testing.T) {
var letterFormat Transform = `Hello! How old are you? {{.age}}? \n-No, I'm {{.grade}}`

func TestShouldTransformDataXMLToLetter(t *testing.T) {
transformed := data.TransformFromXML(inputFormat, letterFormat)
transformed, _ := data.TransformFromXML(inputFormat, letterFormat, nil)
if transformed != `Hello! How old are you? 10? \n-No, I'm 3` {
t.Fail()
}
Expand All @@ -25,7 +25,7 @@ func TestShouldTransformDataXMLToLetter(t *testing.T) {
func TestShouldTransformDataJSONToXML(t *testing.T) {
var json Transform = `{ "first": 10, "second": 2, "third":{"a":1,"b":6,"c":7}, "bla":[1,2,3] }`
var from Transform = `{ "first": "{{age}}", "second": "{{grade}}", "third":{"a":"{{test}}"} }`
transformed := json.TransformFromJSON(from, letterFormat)
transformed, _ := json.TransformFromJSON(from, letterFormat, nil)
if transformed != `Hello! How old are you? 10? \n-No, I'm 2` {
t.Fail()
}
Expand Down
2 changes: 1 addition & 1 deletion unmarshall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package flow

import "errors"

func unmarshallConector(next func(), e *ExchangeMessage, out Message, u Uri, params ...interface{}) error {
func unmarshallConector(next func(), e *ExchangeMessage, out Message, u URI, params ...interface{}) error {
if len(params) < 1 {
return errors.New("You should give a objeto to unmarshall to")
}
Expand Down
Loading

0 comments on commit 134026d

Please sign in to comment.