Skip to content

Commit

Permalink
Define an "AddComments" option veneer
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jan 9, 2025
1 parent 384e44f commit d6d3b05
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/compiler_passes.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ between them.
set_datasource_to_dataquery: {}
```

## `trim_enum_values`

TrimEnumValues removes leading and trailing spaces from string values.
It could happen when they add them by mistake in jsonschema/openapi when they define the enums

### Usage

```yaml
trim_enum_values: {}
```

## `unspec`

Unspec removes the Kubernetes-style envelope added by kindsys.
Expand Down
11 changes: 11 additions & 0 deletions internal/veneers/option/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package option

import (
"fmt"
"strings"

"github.com/grafana/cog/internal/ast"
"github.com/grafana/cog/internal/tools"
Expand Down Expand Up @@ -670,3 +671,13 @@ func AddAssignmentAction(assignment veneers.Assignment) RewriteAction {
return []ast.Option{option}
}
}

// AddCommentsAction adds comments to an option.
func AddCommentsAction(comments []string) RewriteAction {
return func(_ ast.Schemas, builder ast.Builder, option ast.Option) []ast.Option {
option.Comments = append(option.Comments, comments...)
option.AddToVeneerTrail(fmt.Sprintf("AddComments[%s]", strings.Join(comments, " ")))

return []ast.Option{option}
}
}
7 changes: 7 additions & 0 deletions internal/veneers/option/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ func AddAssignment(selector Selector, assignment veneers.Assignment) RewriteRule
Action: AddAssignmentAction(assignment),
}
}

func AddComments(selector Selector, comments []string) RewriteRule {
return RewriteRule{
Selector: selector,
Action: AddCommentsAction(comments),
}
}
19 changes: 19 additions & 0 deletions internal/yaml/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type OptionRule struct {
DisjunctionAsOptions *DisjunctionAsOptions `yaml:"disjunction_as_options"`
Duplicate *DuplicateOption `yaml:"duplicate"`
AddAssignment *AddAssignment `yaml:"add_assignment"`
AddComments *AddComments `yaml:"add_comments"`
}

func (rule OptionRule) AsRewriteRule(pkg string) (option.RewriteRule, error) {
Expand Down Expand Up @@ -76,6 +77,10 @@ func (rule OptionRule) AsRewriteRule(pkg string) (option.RewriteRule, error) {
return rule.AddAssignment.AsRewriteRule(pkg)
}

if rule.AddComments != nil {
return rule.AddComments.AsRewriteRule(pkg)
}

return option.RewriteRule{}, fmt.Errorf("empty rule")
}

Expand Down Expand Up @@ -223,6 +228,20 @@ func (rule AddAssignment) AsRewriteRule(pkg string) (option.RewriteRule, error)
return option.AddAssignment(selector, rule.Assignment), nil
}

type AddComments struct {
OptionSelector `yaml:",inline"`
Comments []string `yaml:"comments"`
}

func (rule AddComments) AsRewriteRule(pkg string) (option.RewriteRule, error) {
selector, err := rule.AsSelector(pkg)
if err != nil {
return option.RewriteRule{}, err
}

return option.AddComments(selector, rule.Comments), nil
}

/******************************************************************************
* Selectors
*****************************************************************************/
Expand Down
26 changes: 26 additions & 0 deletions schemas/veneers.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@
"additionalProperties": false,
"type": "object"
},
"YamlAddComments": {
"properties": {
"by_name": {
"type": "string",
"description": "objectName.optionName"
},
"by_builder": {
"type": "string",
"description": "builderName.optionName\nTODO: ByName should be called ByObject\nand ByBuilder should be called ByName"
},
"by_names": {
"$ref": "#/$defs/YamlByNamesSelector"
},
"comments": {
"items": {
"type": "string"
},
"type": "array"
}
},
"additionalProperties": false,
"type": "object"
},
"YamlAddOption": {
"properties": {
"by_object": {
Expand Down Expand Up @@ -732,6 +755,9 @@
},
"add_assignment": {
"$ref": "#/$defs/YamlAddAssignment"
},
"add_comments": {
"$ref": "#/$defs/YamlAddComments"
}
},
"additionalProperties": false,
Expand Down

0 comments on commit d6d3b05

Please sign in to comment.