ParseMatch will parse the current yaml node as though it is the entry point to a match.
(ctx ParserContext, policy *Policy, node *yaml.Node)
| 847 | |
| 848 | // ParseMatch will parse the current yaml node as though it is the entry point to a match. |
| 849 | func (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 | |
| 890 | func (p *parserImpl) assertYAMLType(id int64, node *yaml.Node, nodeTypes ...yamlNodeType) *yamlNodeType { |
| 891 | nt, found := yamlTypes[node.LongTag()] |
no test coverage detected