func()

in internal/fields/dynamictemplate.go [26:80]


func (d *dynamicTemplate) Matches(currentPath string, definition map[string]any) (bool, error) {
	fullRegex := d.matchPattern == "regex"

	if len(d.match) > 0 {
		name := fieldNameFromPath(currentPath)
		if !slices.Contains(d.match, name) {
			// If there is no an exact match, it is compared with patterns/wildcards
			matches, err := stringMatchesPatterns(d.match, name, fullRegex)
			if err != nil {
				return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
			}

			if !matches {
				return false, nil
			}
		}
	}

	if len(d.unmatch) > 0 {
		name := fieldNameFromPath(currentPath)
		if slices.Contains(d.unmatch, name) {
			return false, nil
		}

		matches, err := stringMatchesPatterns(d.unmatch, name, fullRegex)
		if err != nil {
			return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
		}

		if matches {
			return false, nil
		}
	}

	if len(d.pathMatch) > 0 {
		matches, err := stringMatchesPatterns(d.pathMatch, currentPath, fullRegex)
		if err != nil {
			return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
		}
		if !matches {
			return false, nil
		}
	}

	if len(d.unpathMatch) > 0 {
		matches, err := stringMatchesPatterns(d.unpathMatch, currentPath, fullRegex)
		if err != nil {
			return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
		}
		if matches {
			return false, nil
		}
	}
	return true, nil
}