Skip to content

Commit

Permalink
chore: fix simple bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingrjimsch committed Jun 21, 2024
1 parent f4441a8 commit 255e8d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions api/filters/replacement/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,26 @@ func fieldRetrievalError(fieldPath string, isCreate bool) string {
}

func setFieldValue(options *types.FieldOptions, targetField *yaml.RNode, value *yaml.RNode) error {
var err error
value = value.Copy()
if options != nil && (options.Delimiter != "" || options.FullText != "") {

Check failure on line 258 in api/filters/replacement/replacement.go

View workflow job for this annotation

GitHub Actions / Lint

`if options != nil && (options.Delimiter != "" || options.FullText != "")` has complex nested blocks (complexity: 5) (nestif)
if targetField.YNode().Kind != yaml.ScalarNode {
return fmt.Errorf("delimiter option can only be used with scalar nodes")
}
v := yaml.GetValue(value)
if options.FullText != "" {

Check failure on line 263 in api/filters/replacement/replacement.go

View workflow job for this annotation

GitHub Actions / Lint

ifElseChain: rewrite if-else to switch statement (gocritic)
value.YNode().Value = getByRegex(options.FullText, targetField.YNode().Value, v, options.Index)
v, err = getByRegex(options.FullText, targetField.YNode().Value, v, options.Index)
} else if options.Delimiter != "" && options.EndDelimiter != "" {
regex := regexp.QuoteMeta(options.Delimiter) + `(.*?)` + regexp.QuoteMeta(options.EndDelimiter)
source := options.Delimiter + v + options.EndDelimiter
value.YNode().Value = getByRegex(regex, targetField.YNode().Value, source, options.Index)
v, err = getByRegex(regex, targetField.YNode().Value, source, options.Index)
} else {
value.YNode().Value = getByDelimiter(options.Delimiter, targetField.YNode().Value, v, options.Index)
v = getByDelimiter(options.Delimiter, targetField.YNode().Value, v, options.Index)
}
if err != nil {
return err
}
value.YNode().Value = v
}

if targetField.YNode().Kind == yaml.ScalarNode {
Expand All @@ -294,10 +299,10 @@ func getByDelimiter(delimiter string, target string, source string, index int) s
return strings.Join(tv, delimiter)
}

func getByRegex(regex string, target string, source string, index int) string {
_, err := regexp.Compile(pattern)
func getByRegex(regex string, target string, source string, index int) (string, error) {
_, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("the regex: %s is not valid.", regex)
return "", fmt.Errorf("the regex: %s is not valid.", regex)
}
re := regexp.MustCompile(regex)
counter := 0
Expand Down
2 changes: 1 addition & 1 deletion api/filters/replacement/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3710,7 +3710,7 @@ func TestGetByRegex(t *testing.T) {
},
}
for _, tc := range testCases {
res := getByRegex(tc.regex, tc.target, tc.source, tc.index)
res, _ := getByRegex(tc.regex, tc.target, tc.source, tc.index)
if !assert.Equal(t, tc.want, res) {
t.FailNow()
}
Expand Down

0 comments on commit 255e8d2

Please sign in to comment.