Skip to content

Commit

Permalink
feat (payload.MapOver) transform each item of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
pentateu committed Apr 13, 2020
1 parent 18a249e commit fa770e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions moleculer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Payload interface {
IsArray() bool
IsMap() bool
ForEach(iterator func(key interface{}, value Payload) bool)
MapOver(tranform func(in Payload) Payload) Payload
}

// ActionSchema is used by the validation engine to check if parameters sent to the action are valid.
Expand Down
12 changes: 12 additions & 0 deletions payload/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ func (p *RawPayload) Array() []moleculer.Payload {
return nil
}

func (p *RawPayload) MapOver(transform func(in moleculer.Payload) moleculer.Payload) moleculer.Payload {
if p.IsArray() {
list := []moleculer.Payload{}
for _, value := range p.Array() {
list = append(list, transform(value))
}
return New(list)
} else {
return Error("payload.MapOver can only deal with array payloads.")
}
}

func (p *RawPayload) ForEach(iterator func(key interface{}, value moleculer.Payload) bool) {
if p.IsArray() {
list := p.Array()
Expand Down
12 changes: 12 additions & 0 deletions serializer/jsonSerializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,18 @@ func (payload JSONPayload) ForEach(iterator func(key interface{}, value molecule
})
}

func (p JSONPayload) MapOver(transform func(in moleculer.Payload) moleculer.Payload) moleculer.Payload {
if p.IsArray() {
list := []moleculer.Payload{}
for _, value := range p.Array() {
list = append(list, transform(value))
}
return payload.New(list)
} else {
return payload.Error("payload.MapOver can only deal with array payloads.")
}
}

func (payload JSONPayload) Bool() bool {
return payload.result.Bool()
}
Expand Down

0 comments on commit fa770e6

Please sign in to comment.