MCPcopy Create free account
hub / github.com/cel-expr/cel-go / ParseMatch

Method ParseMatch

policy/parser.go:849–888  ·  view source on GitHub ↗

ParseMatch will parse the current yaml node as though it is the entry point to a match.

(ctx ParserContext, policy *Policy, node *yaml.Node)

Source from the content-addressed store, hash-verified

847
848// ParseMatch will parse the current yaml node as though it is the entry point to a match.
849func (p *parserImpl) ParseMatch(ctx ParserContext, policy *Policy, node *yaml.Node) *Match {
850 m, id := ctx.NewMatch(node)
851 if p.assertYAMLType(id, node, yamlMap) == nil || !p.checkMapValid(ctx, id, node) {
852 return m
853 }
854 m.SetCondition(ValueString{ID: ctx.NextID(), Value: "true"})
855 p.RangeMap(node, func(key, val *yaml.Node) bool {
856 keyID := ctx.CollectMetadata(key)
857 fieldName := key.Value
858 switch fieldName {
859 case "condition":
860 m.SetCondition(ctx.NewString(val))
861 case "output":
862 if m.HasRule() {
863 p.ReportErrorAtID(keyID, "only the rule or the output may be set")
864 }
865 m.SetOutput(ctx.NewString(val))
866 case "explanation":
867 if m.HasRule() {
868 p.ReportErrorAtID(keyID, "explanation can only be set on output match cases, not nested rules")
869 }
870 m.SetExplanation(ctx.NewString(val))
871 case "rule":
872 if m.HasOutput() {
873 p.ReportErrorAtID(keyID, "only the rule or the output may be set")
874 }
875 if m.HasExplanation() {
876 p.ReportErrorAtID(keyID, "explanation can only be set on output match cases, not nested rules")
877 }
878 m.SetRule(p.ParseRule(ctx, policy, val))
879 default:
880 p.visitor.MatchTag(ctx, keyID, fieldName, val, policy, m)
881 }
882 return true
883 })
884 if !m.HasOutput() && !m.HasRule() {
885 p.ReportErrorAtID(id, "match does not specify a rule or output")
886 }
887 return m
888}
889
890func (p *parserImpl) assertYAMLType(id int64, node *yaml.Node, nodeTypes ...yamlNodeType) *yamlNodeType {
891 nt, found := yamlTypes[node.LongTag()]

Callers 1

parseMatchesMethod · 0.95

Calls 15

assertYAMLTypeMethod · 0.95
checkMapValidMethod · 0.95
SetConditionMethod · 0.95
HasRuleMethod · 0.95
ReportErrorAtIDMethod · 0.95
SetOutputMethod · 0.95
SetExplanationMethod · 0.95
HasOutputMethod · 0.95
HasExplanationMethod · 0.95
SetRuleMethod · 0.95
ParseRuleMethod · 0.95
RangeMapMethod · 0.80

Tested by

no test coverage detected