Skip to content

Commit

Permalink
code(pkg/utils/formatter)
Browse files Browse the repository at this point in the history
  • Loading branch information
PxyUp committed Jan 27, 2024
1 parent a24f7a4 commit 52f13ff
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/parser/expression.go → pkg/utils/expression.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/utils/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
humanIndexPlaceHolder = "{HUMAN_INDEX}"
refNamePrefix = "RefName="
envNamePrefix = "FromEnv="
exprNamePrefix = "FromExp="
)

func Format(str string, value builder.Jsonable, index *uint32) string {
Expand All @@ -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 := ""
Expand All @@ -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()
Expand All @@ -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])
Expand All @@ -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 = ""
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 52f13ff

Please sign in to comment.