Skip to content

Commit

Permalink
♻️ Refactor comment template parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 2, 2022
1 parent 671eb78 commit 67cbd8b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions internal/node/has_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (t TmplTag) ToYaml() string {
return "!!" + string(t)
}

const tagSep = ":"

var (
DynamicTag TmplTag = ""
BoolTag TmplTag = "bool"
Expand All @@ -33,16 +35,28 @@ var tags = []TmplTag{
MapTag,
}

// GetCommentTmpl returns the template and tag from a yaml.Node LineComment
func GetCommentTmpl(prefix string, n *yaml.Node) (string, TmplTag) {
comment := n.LineComment
fullPrefix := prefix + " "
if strings.HasPrefix(comment, fullPrefix) {
return comment[len(fullPrefix):], DynamicTag
}
for _, tag := range tags {
fullPrefix := prefix + ":" + string(tag) + " "
if strings.HasPrefix(comment, fullPrefix) {
return comment[len(fullPrefix):], tag
if strings.HasPrefix(comment, prefix) {
// Comment has #yampl prefix
comment = strings.TrimPrefix(comment, prefix)

if strings.HasPrefix(comment, " ") {
// Tag not provided
return comment[1:], DynamicTag
}

if strings.HasPrefix(comment, tagSep) {
// Match comment tag
comment = strings.TrimPrefix(comment, tagSep)

for _, tag := range tags {
prefix := string(tag) + " "
if strings.HasPrefix(comment, prefix) {
return comment[len(prefix):], tag
}
}
}
}
return "", DynamicTag
Expand Down

0 comments on commit 67cbd8b

Please sign in to comment.