goaccessor

command module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 15 Imported by: 0

README

goaccessor

Go Reference

中文介绍

goaccessor is a Go tool designed to automate the generation of getter and setter boilerplate code for your types and variables.

Usage

go install github.com/yujiachen-y/goaccessor@latest

After installing goaccessor, you can use it in the command line interface (CLI) or with //go:generate directives. Check out the Examples section for more information.

Examples

You can find a runnable 'book' example in the example folder.

Consider the file book.go:

package main

//go:generate goaccessor --target Book --getter --setter
type Book struct {
    Title  string
    Author string
}

When we run go generate, it creates a new file book_goaccessor.go:

// Code generated by "goaccessor --target Book --getter --setter". DO NOT EDIT.

package main

func (b *Book) GetTitle() string {
    return b.Title
}

func (b *Book) SetTitle(title string) {
    b.Title = title
}

func (b *Book) GetAuthor() string {
    return b.Author
}

func (b *Book) SetAuthor(author string) {
    b.Author = author
}
Top-level variables

goaccessor isn't just for struct types; it can also handle top-level constants and variables. For instance:

//go:generate goaccessor --target books --getter --setter
var books = map[string]*Book {
    ...
}

After executing go generate, we get:

func GetBooks() map[string]*Book {
    return books
}

func SetBooks(newBooks map[string]*Book) {
    books = newBooks
}
Complex cases

In certain cases, you might want to export specific fields of a top-level variable with a prefix:

//go:generate goaccessor --target bestSellingBook --field --getter --include Author --prefix BestSelling
var bestSellingBook = &Book{ ... }

This directive will generate:

func GetBestSellingAuthor() string {
    return bestSellingBook.Author
}

Options

Here are the available options for goaccessor:

Option Short option Description
--target -t Specify the target to be handled.
--getter -g Generate getter for the target.
--setter -s Generate setter for the target.
--accessor -a Generate both getter and setter for the target.
--pure-getter -pg Generate getter without 'Get' prefix for the target.
--prefix -p Add a prefix to the generated methods/functions.
--field -f Apply the flag (getter, setter, accessor) to each field of the target (only applicable for struct type variables).
--include -i Generate methods only for the specified fields (fields should be comma-separated).
--exclude -e Exclude specified fields from method generation (fields should be comma-separated).

Remember, when using the --pure-getter option, the generated getter methods won't have a 'Get' prefix. For instance, for a Book struct with a Title field, the getter will be Title() instead of GetTitle().

Dependency Management

If you do not want to install goaccessor and want to use it as a dependency for your project, follow these steps:

  1. Go to your project directory and add the goaccessor dependency via go mod:
go get github.com/yujiachen-y/goaccessor@latest
  1. Create a new file named tools.go (or any other name you like) with the following code:
//go:build tools

package main

import (
	_ "github.com/yujiachen-y/goaccessor"
)
  1. If you want to use the goaccessor CLI command, use go run github.com/yujiachen-y/goaccessor@latest instead.

  2. If you use go:generate directives to generate code, change the directives like this:

//go:generate go run github.com/yujiachen-y/goaccessor@latest -target book
var book Book
  1. Note: even though goaccessor is a dependency of your project, it will not—and should not—be a part of your project's build result. We use a build flag in tools.go to ensure goaccessor is ignored during build.

Documentation

Overview

Goaccessor provides a Go tool designed to automate the generation of getter and setter boilerplate code for your data types and variables.

Usage:

go install github.com/yujiachen-y/goaccessor@latest

After installing goaccessor, you can use it in the command line interface (CLI) or with //go:generate directives.

Examples:

Consider the file book.go:

package main

//go:generate goaccessor --target Book --getter --setter
type Book struct {
    Title  string
    Author string
}

When we run go generate, it creates a new file book_goaccessor.go:

package main

func (b *Book) GetTitle() string {
    return b.Title
}

func (b *Book) SetTitle(title string) {
    b.Title = title
}

func (b *Book) GetAuthor() string {
    return b.Author
}

func (b *Book) SetAuthor(author string) {
    b.Author = author
}

goaccessor isn't just for struct types; it can also handle top-level constants and variables. For instance:

//go:generate goaccessor --target books --getter --setter
var books = map[string]*Book {
    ...
}

After executing go generate, we get:

func GetBooks() map[string]*Book {
    return books
}

func SetBooks(newBooks map[string]*Book) {
    books = newBooks
}

In certain cases, you might want to export specific fields of a top-level variable with a prefix:

//go:generate goaccessor --target bestSellingBook --field --getter --include Author --prefix BestSelling
var bestSellingBook = &Book{ ... }

This directive will generate:

func GetBestSellingAuthor() string {
    return bestSellingBook.Author
}

Options:

Here are the available options for goaccessor:

--target | -t: Specify the target to be handled.
--getter | -g: Generate getter for the target.
--setter | -s: Generate setter for the target.
--accessor | -a: Generate both getter and setter for the target.
--prefix | -p: Add a prefix to the generated methods/functions.
--field | -f: Apply the flag (getter, setter, accessor) to each field of the target (only applicable for struct type variables).
--include | -i: Generate methods only for the specified fields (fields should be comma-separated).
--exclude | -e: Exclude specified fields from method generation (fields should be comma-separated).

Dependency Management:

If you do not want to install goaccessor and want to use it as a dependency for your project, follow these steps:

1. Go to your project directory and add the goaccessor dependency via go mod:

go get github.com/yujiachen-y/goaccessor@latest

2. Create a new file named tools.go (or any other name you like) with the following code:

//go:build tools

package main

import (
    _ "github.com/yujiachen-y/goaccessor"
)

3. If you want to use the goaccessor CLI command, use go run github.com/yujiachen-y/goaccessor@latest instead.

4. If you use go:generate directives to generate code, change the directives like this:

//go:generate go run github.com/yujiachen-y/goaccessor@latest -target book
var book Book

5. Note: even though goaccessor is a dependency of your project, it will not—and should not—be a part of your project's build result. We use a build flag in tools.go to ensure goaccessor is ignored during build.

Directories

Path Synopsis
test
fieldtest
fieldtest contains only cases we support.
fieldtest contains only cases we support.
structtest
structtest contains only cases we support.
structtest contains only cases we support.

Jump to

Keyboard shortcuts

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