From c6106f25d7593ff4049b5c523fd099bf5b6b6676 Mon Sep 17 00:00:00 2001 From: miyamo2 Date: Wed, 28 Aug 2024 08:23:55 +0900 Subject: [PATCH] doc: added method description more --- README.md | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 10a3acb..f0b8af3 100644 --- a/README.md +++ b/README.md @@ -135,40 +135,150 @@ Method name is determined by the field name; e.g. `Name` -> `NameEq`. Selects iterator items whose field values are equal to the specified value. +```go +type User struct { + Name string `filtgen:"eq"` +} +``` + +```go +for i, v := range UserSlice(s).NameEq("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxNe` Selects iterator items whose field values are not equal to the specified value. +```go +type User struct { + Name string `filtgen:"ne"` +} +``` + +```go +for i, v := range UserSlice(s).NameNe("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxGt` Selects iterator items whose field values are greater than the specified value. +```go +type User struct { + Name string `filtgen:"gt"` +} +``` + +```go +for i, v := range UserSlice(s).NameGt("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxLt` Selects iterator items whose field values are less than the specified value. +```go +type User struct { + Name string `filtgen:"lt"` +} +``` + +```go +for i, v := range UserSlice(s).NameLt("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxGe` Selects iterator items whose field values are greater than or equal to the specified value. +```go +type User struct { + Name string `filtgen:"ge"` +} +``` + +```go +for i, v := range UserSlice(s).NameGe("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxLe` Selects iterator items whose field values are less than or equal to the specified value. +```go +type User struct { + Name string `filtgen:"le"` +} +``` + +```go +for i, v := range UserSlice(s).NameLe("Alice") { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` + #### `XxxMatches` -Selects iterator items whose field values match the specified function. +Selects iterator items whose field values match the specified function. + +```go +type User struct { + Name string `filtgen:"matches"` +} +``` + +```go +for i, v := range UserSlice(s).NameMatches(func(s string) bool { return strings.HasPrefix(s, "A") }) { + fmt.Printf("%d: %s\n", i, v.Name) +} +``` #### `XxxIs` Selects iterator items whose field values are equal to the specified `error`. Equivalence is determined by `errors.Is`. +```go +type Transaction struct { + ID string `filtgen:"eq"` + Err error `filtgen:"is"` +} +``` + +```go +for i, v := range TransactionSlice(s).ErrIs(fmt.Errorf("something")) { + fmt.Printf("%d: %s\n", i, v.ID) +} +``` + #### `XxxIsnt` -Selects iterator items whose field values are not equal to the specified `error`. +Selects iterator items whose field values are not equal to the specified `error`. Equivalence is determined by `errors.Is`. +```go +type Transaction struct { + ID string `filtgen:"eq"` + Err error `filtgen:"isnt"` +} +``` + +```go +for i, v := range TransactionSlice(s).ErrIsnt(fmt.Errorf("something")) { + fmt.Printf("%d: %s\n", i, v.ID) +} +``` + #### List of compatible filters by type | Type\Filter | `XxxEq` | `XxxNe` | `XxxGt` | `XxxLt` | `XxxGe` | `XxxLe` | `XxxMatches` | `XxxIs` | `XxxIsnt` |