From 52f13ff1f576f02fa0560e65dab6990997726a8a Mon Sep 17 00:00:00 2001 From: PxyUp Date: Sat, 27 Jan 2024 15:26:13 +0400 Subject: [PATCH] code(pkg/utils/formatter) --- README.md | 4 ++-- pkg/notifier/notifier.go | 3 ++- pkg/parser/parser.go | 2 +- pkg/{parser => utils}/expression.go | 5 ++--- pkg/utils/formatter.go | 20 ++++++++++++++++---- pkg/utils/formatter_test.go | 7 +++++++ 6 files changed, 30 insertions(+), 11 deletions(-) rename pkg/{parser => utils}/expression.go (84%) diff --git a/README.md b/README.md index 98a4318..fcffcd2 100644 --- a/README.md +++ b/README.md @@ -884,7 +884,7 @@ type CalculatedConfig struct { - Type - resulting type of expression\ - Expression - expression for calculation (we use [this lib](https://github.com/expr-lang/expr) for calculated expression) -**Predefined values:** +##### Predefined values **fRes** - it is raw(with proper type) result from the parsing [base field](#basefield) @@ -1075,7 +1075,7 @@ Examples: 5. {{{RefName=SomeName}}} - get [reference](#references) value by name. [Example](https://github.com/PxyUp/fitter/blob/master/examples/cli/config_ref.json#L67) 6. {{{RefName=SomeName json.path}}} - get [reference](#references) value by name and extract value by json path. [Example](https://github.com/PxyUp/fitter/blob/master/examples/cli/config_ref.json#L67) 7. {{{FromEnv=ENV_KEY}}} - get value from environment variable - +8. {{{FromExp=fRes + 5 + fIndex}}} - get value from the [expression](https://github.com/expr-lang/expr). [Predefined values](#predefined-values) ## References Special map which **prefetched**(before any processing) and can be user for [connector](#referenceconnectorconfig) or for [placeholder](#placeholder-list) diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index a340043..2f69d6d 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -7,6 +7,7 @@ import ( "github.com/PxyUp/fitter/pkg/config" "github.com/PxyUp/fitter/pkg/logger" "github.com/PxyUp/fitter/pkg/parser" + "github.com/PxyUp/fitter/pkg/utils" "strconv" ) @@ -86,7 +87,7 @@ func ShouldInform(cfg *config.NotifierConfig, result builder.Jsonable) (bool, er return true, nil } - out, err := parser.ProcessExpression(cfg.Expression, result, nil) + out, err := utils.ProcessExpression(cfg.Expression, result, nil) if err != nil { return false, err } diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index e6af00a..fcc5133 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -56,7 +56,7 @@ func (p *ParseResult) ToJson() string { } func getExpressionResult(expr string, fieldType config.FieldType, value builder.Jsonable, index *uint32, logger logger.Logger) builder.Jsonable { - res, err := ProcessExpression(expr, value, index) + res, err := utils.ProcessExpression(expr, value, index) if err != nil { logger.Errorw("error during process calculated field", "error", err.Error()) return builder.NullValue diff --git a/pkg/parser/expression.go b/pkg/utils/expression.go similarity index 84% rename from pkg/parser/expression.go rename to pkg/utils/expression.go index 64ed534..2012608 100644 --- a/pkg/parser/expression.go +++ b/pkg/utils/expression.go @@ -1,8 +1,7 @@ -package parser +package utils import ( "github.com/PxyUp/fitter/pkg/builder" - "github.com/PxyUp/fitter/pkg/utils" "github.com/expr-lang/expr" ) @@ -33,7 +32,7 @@ func extendEnv(env map[string]interface{}, result builder.Jsonable, index *uint3 func ProcessExpression(expression string, result builder.Jsonable, index *uint32) (interface{}, error) { env := extendEnv(defEnv, result, index) - program, err := expr.Compile(utils.Format(expression, result, index), expr.Env(env)) + program, err := expr.Compile(Format(expression, result, index), expr.Env(env)) if err != nil { return false, err } diff --git a/pkg/utils/formatter.go b/pkg/utils/formatter.go index 9dafaa3..d823a4b 100644 --- a/pkg/utils/formatter.go +++ b/pkg/utils/formatter.go @@ -18,6 +18,7 @@ const ( humanIndexPlaceHolder = "{HUMAN_INDEX}" refNamePrefix = "RefName=" envNamePrefix = "FromEnv=" + exprNamePrefix = "FromExp=" ) func Format(str string, value builder.Jsonable, index *uint32) string { @@ -37,10 +38,10 @@ func Format(str string, value builder.Jsonable, index *uint32) string { str = strings.ReplaceAll(str, humanIndexPlaceHolder, fmt.Sprintf("%d", *index+1)) } - return formatJsonPathString(str, value) + return formatJsonPathString(str, value, index) } -func processPrefix(prefix string, value builder.Jsonable) string { +func processPrefix(prefix string, value builder.Jsonable, index *uint32) string { if strings.HasPrefix(prefix, refNamePrefix) { refValue := strings.Split(strings.TrimPrefix(prefix, refNamePrefix), " ") tmp := "" @@ -54,6 +55,16 @@ func processPrefix(prefix string, value builder.Jsonable) string { return builder.PureString(tmp).ToJson() } + if strings.HasPrefix(prefix, exprNamePrefix) { + raw, err := ProcessExpression(strings.TrimPrefix(prefix, exprNamePrefix), value, index) + tmp := "" + if err == nil { + tmp = fmt.Sprintf("%v", raw) + } + + return builder.PureString(tmp).ToJson() + } + if strings.HasPrefix(prefix, envNamePrefix) { envValue := strings.TrimPrefix(prefix, envNamePrefix) return builder.PureString(os.Getenv(envValue)).ToJson() @@ -62,11 +73,12 @@ func processPrefix(prefix string, value builder.Jsonable) string { return gjson.Parse(value.ToJson()).Get(prefix).String() } -func formatJsonPathString(str string, value builder.Jsonable) string { +func formatJsonPathString(str string, value builder.Jsonable, index *uint32) string { new := "" runes := []rune(str) isInJSONPath := false path := "" + for i := 0; i < len(runes); i++ { if !isInJSONPath { new += string(runes[i]) @@ -77,7 +89,7 @@ func formatJsonPathString(str string, value builder.Jsonable) string { if isInJSONPath && strings.HasSuffix(path, jsonPathEnd) { path = strings.TrimSuffix(path, jsonPathEnd) - new += processPrefix(path, value) + new += processPrefix(path, value, index) isInJSONPath = false path = "" diff --git a/pkg/utils/formatter_test.go b/pkg/utils/formatter_test.go index 5b1f2be..4513118 100644 --- a/pkg/utils/formatter_test.go +++ b/pkg/utils/formatter_test.go @@ -22,12 +22,19 @@ func TestRun(t *testing.T) { } func (s *TestFormatterSuite) TestFormatter() { + assert.Equal(s.T(), "", utils.Format("", nil, nil)) + index := uint32(8) assert.Equal(s.T(), "TokenRef=my_token and TokenObjectRef=my_token Object=value kek {\"value\": \"value kek\"} Env=test 8 9", utils.Format("TokenRef={{{RefName=TokenRef}}} and TokenObjectRef={{{RefName=TokenObjectRef token}}} Object={{{value}}} {PL} Env={{{FromEnv=TEST_VAL}}} {INDEX} {HUMAN_INDEX}", builder.Object(map[string]builder.Jsonable{ "value": builder.String("value kek"), }), &index)) } +func (s *TestFormatterSuite) TestExpr() { + index := uint32(1) + assert.Equal(s.T(), "8", utils.Format("{{{FromExp=fRes + 5 + fIndex}}}", builder.Int(2), &index)) +} + func (s *TestFormatterSuite) TearDownSuite() { _ = os.Unsetenv("TEST_VAL") }