-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
246 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package accumulate | ||
|
||
import ( | ||
"context" | ||
"github.com/PaesslerAG/gval" | ||
"github.com/god-jason/bucket/pkg/calc" | ||
"strings" | ||
) | ||
|
||
type Result struct { | ||
Target string | ||
Filter map[string]any | ||
Document map[string]any | ||
} | ||
|
||
type Field struct { | ||
Key string | ||
Value any | ||
|
||
_key gval.Evaluable | ||
_value gval.Evaluable | ||
} | ||
|
||
type Accumulation struct { | ||
Target string `json:"target"` | ||
Filter map[string]any `json:"filter"` | ||
Document map[string]any `json:"document"` | ||
|
||
_target gval.Evaluable | ||
_filter map[string]gval.Evaluable | ||
_fields []*Field | ||
} | ||
|
||
func (a *Accumulation) Init() (err error) { | ||
if expr, has := strings.CutPrefix(a.Target, "="); has { | ||
a._target, err = calc.New(expr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
a._filter = make(map[string]gval.Evaluable) | ||
for key, value := range a.Filter { | ||
switch val := value.(type) { | ||
case string: | ||
if expr, has := strings.CutPrefix(val, "="); has { | ||
a._filter[key], err = calc.New(expr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
default: | ||
} | ||
} | ||
|
||
for key, value := range a.Document { | ||
|
||
f := &Field{Key: key, Value: value} | ||
|
||
//键 | ||
if expr, has := strings.CutPrefix(key, "="); has { | ||
f._key, err = calc.New(expr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
//值 | ||
switch val := value.(type) { | ||
case string: | ||
if expr, has := strings.CutPrefix(val, "="); has { | ||
f._value, err = calc.New(expr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
a._fields = append(a._fields, f) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *Accumulation) Evaluate(args any) (result *Result, err error) { | ||
var ret Result | ||
|
||
//目标 | ||
if a._target != nil { | ||
ret.Target, err = a._target.EvalString(context.Background(), args) | ||
if err != nil { | ||
return | ||
} | ||
} else { | ||
ret.Target = a.Target | ||
} | ||
|
||
//过滤器 | ||
ret.Filter = make(map[string]any) | ||
for key, value := range a._filter { | ||
if value != nil { | ||
ret.Filter[key], err = a._target(context.Background(), args) | ||
if err != nil { | ||
return | ||
} | ||
} else { | ||
ret.Filter[key] = a.Filter[key] | ||
} | ||
} | ||
|
||
ret.Document = make(map[string]any) | ||
|
||
for _, f := range a._fields { | ||
key := f.Key | ||
if f._key != nil { | ||
key, err = f._key.EvalString(context.Background(), args) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
val := f.Value | ||
if f._value != nil { | ||
val, err = f._value.EvalString(context.Background(), args) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
ret.Document[key] = val | ||
} | ||
|
||
return &ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters