diff --git a/moleculer.go b/moleculer.go index eecbe2d7..96c1a348 100644 --- a/moleculer.go +++ b/moleculer.go @@ -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. diff --git a/payload/payload.go b/payload/payload.go index 95042641..7641cf74 100644 --- a/payload/payload.go +++ b/payload/payload.go @@ -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() diff --git a/serializer/jsonSerializer.go b/serializer/jsonSerializer.go index fce7d1e1..557184e8 100644 --- a/serializer/jsonSerializer.go +++ b/serializer/jsonSerializer.go @@ -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() }