Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/snamiki1212/go-gen-slice-accessors

Generate accessor methods for slice in Go
https://github.com/snamiki1212/go-gen-slice-accessors

cli generator go

Last synced: 22 days ago
JSON representation

Generate accessor methods for slice in Go

Awesome Lists containing this project

README

        

# go-gen-slice-accessors

Generate accessor methods for slice.

- Easy Setup & Removal
- Code Generation & Zero Package Size

## Usage

### 1. Install `go-gen-sllice-accessors`

```zsh
$ go install github.com/snamiki1212/go-gen-slice-accessors@latest
$ go-gen-slice-accessors --help
# -> To ensure it was installed correctly, otherwise set up your GOPATH like `export PATH=$PATH:$(go env GOPATH)/bin`
```

### 2. Add `go:generate` directive

```diff filename="user.go"
// user.go
package main

+//go:generate go-gen-slice-accessors --entity User --slice Users --input user.go --output user_gen.go
type User struct {
UserID string
}

type Users []User
```

### 3. Run `go generate` command

```shell
$ go generate user.go
```

```diff filename="user_gen.go"
+// Code generated by go generate DO NOT EDIT.
+
+package main
+
+// UserIDs
+func (xs Users) UserIDs() []string {
+ sli := make([]string, 0, len(xs))
+ for i := range xs {
+ sli = append(sli, xs[i].UserID)
+ }
+ return sli
+}
```

### 4. Enjoy programming

```go
package main

import "fmt"

func main() {
us := Users{{UserID: "1"}, {UserID: "2"}, {UserID: "3"}}
ids := us.UserIDs() // 🚀 You can use accessors for slice.
fmt.Println(ids) // [1 2 3]
}
```

> [!TIP]
> Recommended to install a binary with `go:generate` and skip the installation command but simly run `go generate`.
>
> ```diff
> +//go:generate go install github.com/snamiki1212/go-gen-slice-accessors@latest
> +//go:generate go-gen-slice-accessors --entity User --slice Users --input user.go --output user_gen.go
> type User struct {
> ...
> ```

## Help

```shell
Generate accessor methods for slice.

Usage:
go-gen-slice-accessors [flags]

Flags:
-e, --entity string [required] Target entity name
-x, --exclude strings Field names to exclude
-h, --help help for go-gen-slice-accessors
-m, --import strings Import path name
e.g. --import=time
e.g. --import=time:aliasTime
-i, --input string [required] Input file name
-o, --output string [required] Output file name
-r, --rename strings Rename accessor name
e.g. --rename=Name:GetName
-s, --slice string [required] Target slice name
```

## Examples

- Common case: [input](./example/user.go) → [output](./example/user_gen.go)
- Private field case: [input](./example/private.go) → [output](./example/private_gen.go)
- Exclude flag: [input](./example/exclude.go) → [output](./example/exclude_gen.go)
- Rename flag: [input](./example/rename.go) → [output](./example/rename_gen.go)
- Import flag: [input](./example/imported.go) → [output](./example/imported_gen.go)

## Contribution

### E2E

```shell
$ go generate ./example
$ go run ./example
```

## Alternatives

Please refer to the following package if you would like to generate more.

- [go-gen-lo](https://github.com/snamiki1212/go-gen-lo): Generate samber/lo method for your struct.

## LICENSE

[MIT](./LICENSE)