Skip to content

Commit

Permalink
feat: improve result replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Apr 28, 2024
1 parent 54a588d commit bcb5a62
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package skipper

import (
"fmt"
"reflect"
"regexp"
"strings"

"github.com/davecgh/go-spew/spew"

"github.com/lukasjarosch/skipper/data"
"github.com/lukasjarosch/skipper/expression"

Check failure on line 12 in expression.go

View workflow job for this annotation

GitHub Actions / golangci-lint

could not import github.com/lukasjarosch/skipper/expression (-: # github.com/lukasjarosch/skipper/expression
"github.com/lukasjarosch/skipper/graph"
Expand Down Expand Up @@ -133,6 +136,9 @@ func (pvp pathValueProvider) GetPathValue(path data.Path) (data.Value, error) {
if !ok {
return data.NilValue, fmt.Errorf("path does not exist: %s", path)
}
if d, ok := res.(data.Value); ok {
return d, nil
}
return data.NewValue(res), nil
}

Expand Down Expand Up @@ -230,14 +236,24 @@ func (m *ExpressionManager) ExecuteInput(input string) (data.Value, error) {
if len(exprOffsets) == 0 {
continue
}

// in case the sourceValue only consists of the expression, we can
// use raw value replacement instead of string replacement in all other cases.
if exprOffsets[0] == 0 && len(sourceValue.String()) == exprOffsets[1] {
valueProvider[pathVertex] = result.(reflect.Value).Interface()
continue
}

// perform string replacement
oldValue := sourceValue.String()[exprOffsets[0]:exprOffsets[1]]
newValue := strings.Replace(sourceValue.String(), oldValue, data.NewValue(result).String(), 1)

// update the valueProvider with the new value
valueProvider[pathVertex] = newValue
spew.Println("ASDF", newValue)
}
}

spew.Dump(valueProvider)

return valueProvider.GetPathValue(data.NewPath(tmpVertexHash))
}

Expand Down

0 comments on commit bcb5a62

Please sign in to comment.