parser

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractFieldNames added in v1.1.0

func ExtractFieldNames(exprStr string) []string

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

func ExtractSheetInfo(url string) (string, string, error)

ExtractSheetInfo extracts the Google Sheets document ID and GID (sheet/tab ID) from a given URL.

Supported Google Sheets URL formats:

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

func IsValidEmail(email string) bool

IsValidEmail returns true if the email address is valid.

func ValidateEmail added in v1.1.0

func ValidateEmail(email string) error

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

type Expression interface {
	Evaluate(data map[string]string) bool
}

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

type Recipient struct {
	Email string
	Data  map[string]string
}

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

func ParseCSV(path string) ([]Recipient, error)

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

func ParseCSVFromReader(reader io.Reader) ([]Recipient, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL