Skip to content

Commit

Permalink
fix: add forgotten helpers.go
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
  • Loading branch information
FUSAKLA committed Mar 8, 2024
1 parent cf81e1a commit ce7546a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions pkg/unmarshaler/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package unmarshaler

import (
"strings"

"gopkg.in/yaml.v3"
)

func getYamlNodeComments(n yaml.Node, commentPrefix string) []string {
comments := []string{}
for _, line := range strings.Split(n.HeadComment, "\n") {
if !strings.Contains(line, commentPrefix) {
continue
}
comments = append(comments, line)
}
return comments
}

func getExpressionComments(expr, commentPrefix string) []string {
comments := []string{}
for _, line := range strings.Split(expr, "\n") {
before, comment, found := strings.Cut(line, "#")
if !found || strings.TrimSpace(before) != "" {
continue
}
if !strings.Contains(comment, commentPrefix) {
continue
}
comments = append(comments, comment)
}
return comments
}

func disabledValidatorsFromComments(comments []string, commentPrefix string) []string {
commentPrefix += ":"
disabledValidators := []string{}
for _, comment := range comments {
_, csv, found := strings.Cut(comment, commentPrefix)
if !found {
continue
}
validators := strings.Split(csv, ",")
for _, v := range validators {
vv := strings.TrimSpace(v)
disabledValidators = append(disabledValidators, vv)
}
}
return disabledValidators
}

func unmarshalToNodeAndStruct(value, dstNode *yaml.Node, dstStruct interface{}) error {
err := value.Decode(dstNode)
if err != nil {
return err
}
err = value.Decode(dstStruct)
if err != nil {
return err
}
return nil
}

0 comments on commit ce7546a

Please sign in to comment.