query

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package query provides functions to query letters from a store.

Index

Examples

Constants

View Source
const (
	// SortAsc sorts letters in ascending order.
	SortAsc = SortDirection(iota)
	// SortDesc sorts letters in descending order.
	SortDesc
)
View Source
const (
	// SortBySendDate sorts letters by the "SentAt" field.
	SortBySendDate = Sorting(iota)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AttachmentFilter

type AttachmentFilter struct {
	Names        []string
	ContentTypes []string
	Size         AttachmentSizeFilter
}

AttachmentFilter filters letters by their attachments.

type AttachmentSizeFilter

type AttachmentSizeFilter struct {
	Exact  []int
	Ranges [][2]int
}

AttachmentSizeFilter filters letters by their attachment sizes.

type Cursor

type Cursor interface {
	Next(context.Context) bool
	Current() store.Letter
	Close(context.Context) error
	Err() error
}

Cursor is used to iterate over a stream of letters.

func Run

func Run(ctx context.Context, repo Repository, opts ...Option) (Cursor, error)

Run builds and runs a query against repo. Use opts to configure the query.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
	"github.com/bounoable/postdog/plugin/store/query"
)

func main() {
	// Usually this would be a persistent implementation
	repo := memorystore.New(
		store.Letter{Letter: letter.Write(letter.Subject("Letter 1"))},
		store.Letter{Letter: letter.Write(letter.Subject("Letter 2"))},
	)

	cur, err := query.Run(
		context.Background(),
		repo,
		query.Subject("Letter"), // sender name / address must contain "Letter"
		query.Sort(query.SortBySendDate, query.SortDesc), // sort descending by send date
		// more query options ...
	)
	defer cur.Close(context.Background())

	if err != nil {
		panic(err)
	}

	for cur.Next(context.Background()) {
		let := cur.Current()
		fmt.Println(let)
	}

	if cur.Err() != nil {
		panic(cur.Err())
	}
}
Output:

type Option

type Option func(*Query)

Option is a query option.

func AttachmentContentType

func AttachmentContentType(ct ...string) Option

AttachmentContentType includes only letters that have an attachment whose "Content-Type" header contains ct.

func AttachmentName

func AttachmentName(n ...string) Option

AttachmentName includes only letters that have an attachment whose name contains n.

func AttachmentSize

func AttachmentSize(s ...int) Option

AttachmentSize includes only letters that have an attachment whose filesize is exactly s.

func AttachmentSizeRange

func AttachmentSizeRange(min, max int) Option

AttachmentSizeRange includes only letters that have an attachment whose filesize is in the range [min, max].

func BCC

func BCC(bcc ...string) Option

BCC includes only letters whose "BCC" recipients' names or addresses contains bcc.

func CC

func CC(cc ...string) Option

CC includes only letters whose "CC" recipients' names or addresses contains cc.

func From

func From(f ...string) Option

From includes only letters whose sender's name or address contains f.

func SentAfter

func SentAfter(t time.Time) Option

SentAfter includes only letters which have been sent after t.

func SentBefore

func SentBefore(t time.Time) Option

SentBefore includes only letters which have been sent before t.

func SentBetween

func SentBetween(l, r time.Time) Option

SentBetween includes only letters which have been sent between [l, r].

func SentInBetween

func SentInBetween(l, r time.Time) Option

SentInBetween includes only letters which have been sent between (l, r).

func Sort

func Sort(by Sorting, dir SortDirection) Option

Sort configures the sorting of the letters.

func Subject

func Subject(s ...string) Option

Subject includes only letters whose subject contains s.

func To

func To(to ...string) Option

To includes only letters whose "To" recipient's name or address contains to.

type Query

type Query struct {
	SentAt     SentAtFilter
	Subjects   []string
	From       []string
	To         []string
	CC         []string
	BCC        []string
	Attachment AttachmentFilter
	Sort       SortConfig
}

Query defines the filters and options for a query.

func New

func New(opts ...Option) Query

New builds and returns a new Query, configured by opts.

type Repository

type Repository interface {
	Query(context.Context, Query) (Cursor, error)
}

Repository is the query repository.

type SentAtFilter

type SentAtFilter struct {
	Before time.Time
	After  time.Time
}

SentAtFilter filters letters by their send date.

type SortConfig

type SortConfig struct {
	SortBy Sorting
	Dir    SortDirection
}

SortConfig defines the sorting of queried letters.

type SortDirection

type SortDirection int

SortDirection is the direction of the sorting.

type Sorting

type Sorting int

Sorting is the sorting of letters.

Directories

Path Synopsis
Package mock_query is a generated GoMock package.
Package mock_query is a generated GoMock package.

Jump to

Keyboard shortcuts

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