Files
summercms/modules/sieve/example_test.go

183 lines
4.8 KiB
Go

package sieve_test
import (
"fmt"
"slices"
"strconv"
"git.golem15.com/golem15/summercms/modules/sieve"
)
type post struct {
ID int
Author string
Title string
}
var posts = []post{
{ID: 1, Author: "ana", Title: "Hello"},
{ID: 2, Author: "bob", Title: "Summer"},
{ID: 3, Author: "ana", Title: "Goodbye"},
}
func ExampleMap() {
posts := []post{
{ID: 1, Title: "Hello"},
{ID: 2, Title: "Summer"},
{ID: 3, Title: "Goodbye"},
}
// Pluck the titles with a field accessor.
titles := sieve.Map(posts, func(p post) string { return p.Title })
fmt.Println(titles)
// Output: [Hello Summer Goodbye]
}
func ExampleTryMap() {
nums, err := sieve.TryMap([]string{"1", "2", "3"}, strconv.Atoi)
fmt.Println(nums, err)
nums, err = sieve.TryMap([]string{"1", "x", "3"}, strconv.Atoi)
fmt.Println(nums, err)
// Output:
// [1 2 3] <nil>
// [] strconv.Atoi: parsing "x": invalid syntax
}
func ExampleFilter() {
byAna := sieve.Filter(posts, func(p post) bool { return p.Author == "ana" })
fmt.Println(len(byAna), byAna[0].Title, byAna[1].Title)
// Output: 2 Hello Goodbye
}
func ExampleReduce() {
total := sieve.Reduce([]int{1, 2, 3, 4}, 0, func(sum, v int) int { return sum + v })
fmt.Println(total)
// Output: 10
}
func ExampleKeyBy() {
byID := sieve.KeyBy(posts, func(p post) int { return p.ID })
fmt.Println(byID[2].Title, len(byID))
// Output: Summer 3
}
func ExampleGroupBy() {
byAuthor := sieve.GroupBy(posts, func(p post) string { return p.Author })
titles := make(map[string][]string, len(byAuthor))
for author, group := range byAuthor {
titles[author] = sieve.Map(group, func(p post) string { return p.Title })
}
fmt.Println(titles)
// Output: map[ana:[Hello Goodbye] bob:[Summer]]
}
func ExampleUniq() {
fmt.Println(sieve.Uniq([]int{3, 1, 3, 2, 1}))
// Output: [3 1 2]
}
func ExampleUniqBy() {
firstPerAuthor := sieve.UniqBy(posts, func(p post) string { return p.Author })
fmt.Println(sieve.Map(firstPerAuthor, func(p post) string { return p.Title }))
// Output: [Hello Summer]
}
func ExamplePartition() {
even, odd := sieve.Partition([]int{1, 2, 3, 4, 5}, func(v int) bool { return v%2 == 0 })
fmt.Println(even, odd)
// Output: [2 4] [1 3 5]
}
func ExampleFind() {
p, ok := sieve.Find(posts, func(p post) bool { return p.Author == "bob" })
fmt.Println(p.Title, ok)
_, ok = sieve.Find(posts, func(p post) bool { return p.Author == "acme" })
fmt.Println(ok)
// Output:
// Summer true
// false
}
func ExampleSetOf() {
tags := sieve.SetOf("blog", "news", "blog")
fmt.Println(len(tags))
// Output: 2
}
func ExampleSet_Has() {
tags := sieve.SetOf("blog", "news")
fmt.Println(tags.Has("blog"), tags.Has("acme"))
// Output: true false
}
func ExampleSet_Add() {
tags := sieve.SetOf[string]()
tags.Add("blog", "news", "blog")
fmt.Println(len(tags), tags.Has("news"))
// Output: 2 true
}
func ExampleSet_Slice() {
ids := sieve.SetOf(3, 1, 2, 3).Slice()
slices.Sort(ids)
fmt.Println(ids)
// Output: [1 2 3]
}
// blogPost is the model that shape filters, groups and reshapes.
type blogPost struct {
ID int
Author string
Title string
Published bool
}
// postResponse is the JSON shape of one post in a list response.
type postResponse struct {
ID int `json:"id"`
Title string `json:"title"`
}
func shape(posts []blogPost, rawIDs []string) error {
// Keep the published posts and turn them into response structs.
published := sieve.Filter(posts, func(p blogPost) bool { return p.Published })
body := sieve.Map(published, func(p blogPost) postResponse {
return postResponse{ID: p.ID, Title: p.Title}
})
// Key by ID for lookups, group by author, keep one post per author.
byID := sieve.KeyBy(posts, func(p blogPost) int { return p.ID })
byAuthor := sieve.GroupBy(posts, func(p blogPost) string { return p.Author })
firstPerAuthor := sieve.UniqBy(posts, func(p blogPost) string { return p.Author })
// Split drafts from published posts, and find the first draft.
live, drafts := sieve.Partition(posts, func(p blogPost) bool { return p.Published })
firstDraft, found := sieve.Find(posts, func(p blogPost) bool { return !p.Published })
// Parse IDs from a query string, stopping at the first bad one.
ids, err := sieve.TryMap(rawIDs, strconv.Atoi)
if err != nil {
return fmt.Errorf("blog: bad post id: %w", err)
}
wanted := sieve.SetOf(ids...)
fmt.Println(len(body), len(byID), len(byAuthor), len(firstPerAuthor),
len(live), len(drafts), firstDraft.Title, found, wanted.Has(1))
return nil
}
func Example_shape() {
posts := []blogPost{
{ID: 1, Author: "ana", Title: "Hello", Published: true},
{ID: 2, Author: "bob", Title: "Summer"},
{ID: 3, Author: "ana", Title: "Goodbye", Published: true},
}
fmt.Println(shape(posts, []string{"1", "3"}))
fmt.Println(shape(posts, []string{"1", "x"}))
// Output:
// 2 3 2 2 2 1 Summer true true
// <nil>
// blog: bad post id: strconv.Atoi: parsing "x": invalid syntax
}