Documentation
¶
Overview ¶
Package parser provides CSV recipient filtering using logical expressions.
This file implements the high-level Filter() function that: - Applies a logical expression to each recipient - Normalizes field names to lowercase - Supports email fallback field
Index ¶
- func ExtractFieldNames(exprStr string) []string
- func ExtractSheetInfo(url string) (string, string, error)
- func GetSheetCSVStream(sheetURL string) (io.ReadCloser, error)
- func IsValidEmail(email string) bool
- func ValidateEmail(email string) error
- func ValidateFields(exp Expression, recipients []Recipient) error
- type Expression
- type Recipient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractFieldNames ¶ added in v1.1.0
ExtractFieldNames extracts potential field names from an expression string. This is a best-effort extraction for informational purposes.
func ExtractSheetInfo ¶ added in v1.1.0
ExtractSheetInfo extracts the Google Sheets document ID and GID (sheet/tab ID) from a given URL.
Supported Google Sheets URL formats:
- https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j/edit#gid=123456789
- https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j/export?format=csv&gid=987654321
- https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j
Parameters:
- url: The full URL of a Google Sheets document.
Returns:
- id: The document ID (string)
- gid: The sheet/tab GID (string). Defaults to "0" if not found
- error: If the URL format is invalid
func GetSheetCSVStream ¶
func GetSheetCSVStream(sheetURL string) (io.ReadCloser, error)
GetSheetCSVStream fetches a Google Sheet as a CSV stream. The URL must be a Google Sheets URL (docs.google.com) — arbitrary URLs are rejected to prevent SSRF. Open redirects are blocked by the HTTP client.
func IsValidEmail ¶ added in v1.1.0
IsValidEmail returns true if the email address is valid.
func ValidateEmail ¶ added in v1.1.0
ValidateEmail checks if an email address is valid using net/mail. Returns nil if valid, error otherwise.
func ValidateFields ¶ added in v1.1.0
func ValidateFields(exp Expression, recipients []Recipient) error
ValidateFields ensures all fields used in the expression exist in the CSV data. It extracts field names from the expression and checks them against the first recipient. Fields like "email" (injected at filter time) are also accepted. Note: This is informational only - the expr library handles undefined variables gracefully by returning false.
Types ¶
type Expression ¶ added in v1.1.0
Expression is the interface for evaluating filter expressions. It wraps the expr library for boolean evaluation against recipient data.
func MustParseExpression ¶ added in v1.1.0
func MustParseExpression(input string) Expression
MustParseExpression is like ParseExpression but panics on error. Useful for testing.
func ParseExpression ¶ added in v1.1.0
func ParseExpression(input string) (Expression, error)
ParseExpression compiles a filter expression string into an evaluable Expression.
Supported syntax:
- Comparison: ==, !=, <, <=, >, >=
- String contains: contains(field, "value") or field contains "value"
- String prefix: startsWith(field, "value") or field startsWith "value"
- String suffix: endsWith(field, "value") or field endsWith "value"
- Logical operators: &&, ||, !
- Parentheses for grouping: (a == b && c == d)
All string comparisons are case-insensitive.
Example expressions:
- name == "John"
- company != "Acme" && salary > 50000
- contains(location, "York") || startsWith(email, "admin")
- location contains "York" || email startsWith "admin"
type Recipient ¶
func Filter ¶
func Filter(recipients []Recipient, exp Expression) []Recipient
Filter applies the provided logical EXPRESSION to a slice of recipients. It returns only those recipients for whom the expression evaluates to true.
The expression is evaluated case-insensitively, and field names are normalized to lowercase.
func ParseCSV ¶
ParseCSV reads a CSV file from a given path and returns a list of Recipients. This is a wrapper around ParseCSVFromReader for convenience.
func ParseCSVFromReader ¶
ParseCSVFromReader reads a CSV from any io.Reader and returns a list of Recipients. It expects one column to be named 'email' and uses other columns as dynamic data. Invalid email addresses are skipped with a warning.