Skip to content

Commit 47f764e

Browse files
committed
ast: add feature registration from the outside
If I add this to `main.go` (imagine another executable including OPA), ```diff diff --git a/main.go b/main.go index 9d5aea6..5126479107 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,11 @@ "os" "github.com/open-policy-agent/opa/cmd" + "github.com/open-policy-agent/opa/v1/ast" ) func main() { + ast.RegisterFeatures("foo", "bar", "baz") if err := cmd.RootCommand.Execute(); err != nil { os.Exit(1) } ``` and I subsequently run `opa capabilities --current`, we'll find (other stuff omitted): ```json { "features": [ "bar", "baz", "foo", "keywords_in_refs", "rego_v1" ] } ``` Signed-off-by: Stephan Renatus <stephan@styra.com>
1 parent 5430c72 commit 47f764e

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

v1/ast/capabilities.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ const FeatureRegoV1 = "rego_v1"
5757
const FeatureRegoV1Import = "rego_v1_import"
5858
const FeatureKeywordsInRefs = "keywords_in_refs"
5959

60+
// Features carries the default features supported by this version of OPA.
61+
// Use RegisterFeatures to add to them.
62+
var Features = []string{
63+
FeatureRegoV1,
64+
FeatureKeywordsInRefs,
65+
}
66+
67+
// RegisterFeatures lets applications wrapping OPA register features, to be
68+
// included in `ast.CapabilitiesForThisVersion()`.
69+
func RegisterFeatures(fs ...string) {
70+
for i := range fs {
71+
if slices.Contains(Features, fs[i]) {
72+
continue
73+
}
74+
Features = append(Features, fs[i])
75+
}
76+
}
77+
6078
// Capabilities defines a structure containing data that describes the capabilities
6179
// or features supported by a particular version of OPA.
6280
type Capabilities struct {
@@ -141,10 +159,8 @@ func CapabilitiesForThisVersion(opts ...CapabilitiesOption) *Capabilities {
141159
f.FutureKeywords = append(f.FutureKeywords, kw)
142160
}
143161

144-
f.Features = []string{
145-
FeatureRegoV1,
146-
FeatureKeywordsInRefs,
147-
}
162+
f.Features = make([]string, len(Features))
163+
copy(f.Features, Features)
148164
}
149165

150166
sort.Strings(f.FutureKeywords)

0 commit comments

Comments
 (0)