{"id":24638237,"url":"https://github.com/miyamo2/filtgen","last_synced_at":"2025-07-14T04:13:33.350Z","repository":{"id":255079717,"uuid":"848285953","full_name":"miyamo2/filtgen","owner":"miyamo2","description":"generate an iterator by go generate that can filter by field values of the item.","archived":false,"fork":false,"pushed_at":"2025-05-07T16:02:29.000Z","size":81,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-08T23:44:17.759Z","etag":null,"topics":["cli","code-generation","codegen","filter","filtering","go","gogenerate","golang","iterator","iterator-library","rangefunc"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miyamo2.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-27T13:38:41.000Z","updated_at":"2025-02-18T10:31:55.000Z","dependencies_parsed_at":"2024-12-16T15:23:07.600Z","dependency_job_id":"2c87afb0-6eaf-4f69-8c62-49872d4e7a53","html_url":"https://github.com/miyamo2/filtgen","commit_stats":null,"previous_names":["miyamo2/filtgen"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miyamo2%2Ffiltgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miyamo2%2Ffiltgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miyamo2%2Ffiltgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miyamo2%2Ffiltgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miyamo2","download_url":"https://codeload.github.com/miyamo2/filtgen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166473,"owners_count":21864467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","code-generation","codegen","filter","filtering","go","gogenerate","golang","iterator","iterator-library","rangefunc"],"created_at":"2025-01-25T10:13:30.947Z","updated_at":"2025-05-08T23:44:24.430Z","avatar_url":"https://github.com/miyamo2.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# filtgen\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/miyamo2/filtgen.svg)](https://pkg.go.dev/github.com/miyamo2/filtgen)\n[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/miyamo2/filtgen)](https://img.shields.io/github/go-mod/go-version/miyamo2/filtgen)\n[![GitHub release (latest by date)](https://img.shields.io/github/v/release/miyamo2/filtgen)](https://img.shields.io/github/v/release/miyamo2/filtgen)\n[![ci](https://github.com/miyamo2/filtgen/actions/workflows/ci.yaml/badge.svg)](https://github.com/miyamo2/filtgen/actions/workflows/ci.yaml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/miyamo2/filtgen)](https://goreportcard.com/report/github.com/miyamo2/filtgen)\n[![GitHub License](https://img.shields.io/github/license/miyamo2/filtgen?\u0026color=blue)](https://img.shields.io/github/license/miyamo2/filtgen?\u0026color=blue)\n\nCLI tool for generating iterators that selects items by their field values.\n\n## Quick Start\n\n### Install\n\n```sh\ngo install github.com/miyamo2/filtgen@latest\n```\n\n### Usage\n\n#### Step 1: Tag your struct with the `filtgen`\n\n```go\n//go:generate filtgen generate -s $GOFILE\npackage main\n\nimport (\n\t\"time\"\n)\n\ntype Foo struct {\n\tStringField     string     `filtgen:\"*\"`\n\tIntField        int        `filtgen:\"*\"`\n\tBoolField       bool       `filtgen:\"*\"`\n\tTimeField       time.Time  `filtgen:\"*\"`\n\tErrorField      error      `filtgen:\"*\"`\n}\n```\n\n#### Step 2: Run `filtgen generate`\n\n##### Run with `go generate`\n\n```sh\ngo generate ./...\n```\n\n##### Run with `filtgen` command\n\n```sh\nfiltgen generate -s your_struct.go\n```\n\n#### Step 3: Use generated iterator\n\n```go\nerrSomething := errors.New(\"something\")\n\ns := []Foo{\n    {StringField: \"a\", IntField: 1, BoolField: true, TimeField: time.Now()},\n    {StringField: \"b\", IntField: 2, BoolField: false, TimeField: time.Now(), ErrorField: errSomething},\n    {StringField: \"c\", IntField: 3, BoolField: true, TimeField: time.Now().Add(-(time.Hour * 2))},\n}\n\nfor i, v := range FooSlice(s).StringFieldGe(\"a\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 0: a\n// 1: b\n// 2: c\n\nfor i, v := range FooSlice(s).IntFieldGt(1) {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 1: b\n// 2: c\n\nfor i, v := range FooSlice(s).BoolFieldEq(true) {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 0: a\n// 2: c\n\nfor i, v := range FooSlice(s).TimeFieldMatches(func(t time.Time) bool { return t.Before(time.Now()) }) {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 2: c\n\nfor i, v := range FooSlice(s).ErrorFieldIs(errSomething) {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 1: b\n\n// with method chaining\nfor i, v := range FooSlice(s).IntFieldGt(1).StringFieldNe(\"c\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.StringField)\n}\n// Output: 1: b\n```\n\nFor the actual generated code, see the [example](https://github.com/miyamo2/filtgen/tree/main/example).\n\n## What codes to be generated?\n\n### Types\n\n`filtgen` generates the following defined-types.  \n\n- `XxxSlice`(`[]T`)\n\n- `XxxMap[K]`(`map[K compareble]T`)\n\n- `XxxSeq[T]`(`iter.Seq[T]`)\n\n- `XxxSeq2[T]`(`iter.Seq2[T, U]`)\n\nType name is determined by the struct name; e.g. `User` -\u003e `UserSlice`.  \n\nTo use the generated methods, cast must be performed.\n```go\ns := []User{\n    {Name: \"Alice\"},\n    {Name: \"Bob\"},\n}\nfor i, v := range UserSlice(s).NameEq(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n### Methods\n\nFollowing methods are generated by `filtgen`.  \n\n- [`XxxEq`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxeq)\n- [`XxxNe`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxne)\n- [`XxxGt`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxgt)\n- [`XxxLt`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxlt)\n- [`XxxGe`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxge)\n- [`XxxLe`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxle)\n- [`XxxMatches`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxmatches)\n- [`XxxIs`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxis)\n- [`XxxIsnt`](https://github.com/miyamo2/filtgen/tree/main/README.md#xxxisnt)\n\nMethod name is determined by the field name; e.g. `Name` -\u003e `NameEq`.\n\n#### `XxxEq`\n\nSelects iterator items whose field values are equal to the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"eq\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameEq(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxNe`\n\nSelects iterator items whose field values are not equal to the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"ne\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameNe(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxGt`\n\nSelects iterator items whose field values are greater than the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"gt\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameGt(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxLt`\n\nSelects iterator items whose field values are less than the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"lt\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameLt(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxGe`\n\nSelects iterator items whose field values are greater than or equal to the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"ge\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameGe(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxLe`\n\nSelects iterator items whose field values are less than or equal to the specified value.\n\n```go\ntype User struct {\n    Name string `filtgen:\"le\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameLe(\"Alice\") {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxMatches`\n\nSelects iterator items whose field values match the specified function.  \n\n```go\ntype User struct {\n    Name string `filtgen:\"matches\"`\n}\n```\n\n```go\nfor i, v := range UserSlice(s).NameMatches(func(s string) bool { return strings.HasPrefix(s, \"A\") }) {\n    fmt.Printf(\"%d: %s\\n\", i, v.Name)\n}\n```\n\n#### `XxxIs`\n\nSelects iterator items whose field values are equal to the specified `error`.  \nEquivalence is determined by `errors.Is`.\n\n```go\ntype Transaction struct {\n    ID   string `filtgen:\"eq\"`\n    Err  error  `filtgen:\"is\"`\n}\n```\n\n```go\nfor i, v := range TransactionSlice(s).ErrIs(fmt.Errorf(\"something\")) {\n    fmt.Printf(\"%d: %s\\n\", i, v.ID)\n}\n```\n\n#### `XxxIsnt`\n\nSelects iterator items whose field values are not equal to the specified `error`.  \nEquivalence is determined by `errors.Is`.\n\n```go\ntype Transaction struct {\n    ID   string `filtgen:\"eq\"`\n    Err  error  `filtgen:\"isnt\"`\n}\n```\n\n```go\nfor i, v := range TransactionSlice(s).ErrIsnt(fmt.Errorf(\"something\")) {\n    fmt.Printf(\"%d: %s\\n\", i, v.ID)\n}\n```\n\n#### List of compatible filters by type\n\n| Type\\Filter  | `XxxEq` | `XxxNe` | `XxxGt` | `XxxLt` | `XxxGe` | `XxxLe` | `XxxMatches` | `XxxIs` | `XxxIsnt` |\n|--------------|---------|---------|---------|---------|---------|---------|--------------|---------|-----------|\n| `string`     | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `int`        | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `int8`       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `int16`      | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `int32`      | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `int64`      | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `uint`       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `uint8`      | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `uint16`     | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `uint32`     | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `uint64`     | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `float32`    | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `float64`    | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `complex64`  | ❌       | ❌       | ❌       | ❌       | ❌       | ❌       | ✅            | ❌       | ❌         |\n| `complex128` | ❌       | ❌       | ❌       | ❌       | ❌       | ❌       | ✅            | ❌       | ❌         |\n| `byte`       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `rune`       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅       | ✅            | ❌       | ❌         |\n| `error`      | ❌       | ❌       | ❌       | ❌       | ❌       | ❌       | ✅            | ✅       | ✅         |\n| `bool`       | ✅       | ✅       | ❌       | ❌       | ❌       | ❌       | ✅            | ❌       | ❌         |\n| other-types  | ❌       | ❌       | ❌       | ❌       | ❌       | ❌       | ✅            | ❌       | ❌         |\n\n### `filtgen` tags\n\nThe following values can be set in the `filtgen` tag.  \nIf you want to set multiple values, separate them with `,`.\n\n```go\ntype A struct {\n    StringField string `filtgen:\"eq,ne\"`\n}\n```\n\n| Value     | Description                                                         |\n|-----------|---------------------------------------------------------------------|\n| `*`       | Generate all methods supported according to the type of that field. |\n| `eq`      | Generate `XxxEq` methods.                                           |\n| `ne`      | Generate `XxxNe` methods.                                           |\n| `gt`      | Generate `XxxGt` methods.                                           |\n| `lt`      | Generate `XxxLt` methods.                                           |\n| `ge`      | Generate `XxxGe` methods.                                           |\n| `le`      | Generate `XxxLe` methods.                                           |\n| `matches` | Generate `XxxMatches` methods.                                      |\n| `is`      | Generate `XxxIs` methods.                                           |\n| `isnt`    | Generate `XxxIsnt` methods.                                         |\n\n## For Contributors\n\nFeel free to open a PR or an Issue.  \nHowever, you must promise to follow our [Code of Conduct](https://github.com/miyamo2/filtgen/blob/main/CODE_OF_CONDUCT.md).\n\n### Tasks\n\nWe recommend that this section be run with [`xc`](https://github.com/joerdav/xc).\n\n#### setup:deps\n\nInstall `golangci-lint`.\n\n```sh\ngo install github.com/golangci/golangci-lint/cmd/golangci-lint@latest\n```\n\n#### lint\n\n```sh\ngolangci-lint run --fix\n```\n\n## License\n\n**filtgen** released under the [MIT License](https://github.com/miyamo2/filtgen/blob/main/LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiyamo2%2Ffiltgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiyamo2%2Ffiltgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiyamo2%2Ffiltgen/lists"}