pinfomap

package module
v0.0.2024020954 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

pinfomap

Description

Generates Go code from a template by referencing package information such as package names, imports, and struct fields.

Examples

Accessor Generation

./foo.go contains the sample struct Foo:

package examples

import (
	goimports "github.com/incu6us/goimports-reviser/v3/reviser"
	"text/template"
)

type Foo struct {
	bar  string `accessor:"setter,getter=true"`
	Baz  int
	qux  template.Template    `accessor:"getter,setter=false"`
	quux *goimports.SourceDir `accessor:"setter"`
}

./gen_foo_accessor_go/main.go serves as an accessor generator for the struct. You can place the generator comment //go:generate go run ./gen_foo_accessor_go/ in the same directory as ./foo.go:

package main

import (
	"github.com/knaka/go-pinfomap"
	"github.com/knaka/go-pinfomap/examples"
	"github.com/knaka/go-pinfomap/generator"

	"log"
)

func main() {
	// Extracts information about a struct by passing an instance of the struct.
	// Here, we're using 'examples.Foo{}' with an option to include fields tagged as "accessor".
	structInfo, err := pinfomap.NewStructInfo(examples.Foo{}, pinfomap.WithTags("accessor"))
	if err != nil {
		log.Fatalf("failed to get struct info: %v", err)
	}

	// You can also specify a struct by its name to extract information.
	// Use just the name "Foo" for structs in the current package or provide the full path
	// like "github.com/knaka/foo/bar.baz" for the other structs.
	// This approach is useful for accessing structs dynamically or when they're not directly accessible.
	_, err = pinfomap.NewStructInfo("Foo", pinfomap.WithTags("accessor"))
	if err != nil {
		log.Fatalf("failed to get struct info: %v", err)
	}

	// Adding additional data to the 'structInfo' which can be utilized in the template.
	structInfo.Data = map[string]any{
		"AdditionalComment": "This is a comment.",
	}

	// Generates Go code based on the 'AccessorTemplate' and the provided 'structInfo'.
	err = generator.GenerateGo(pinfomap.AccessorTemplate, structInfo)
	if err != nil {
		log.Fatalf("failed to generate go: %v", err)
	}
}

Running go run ./gen_foo_accessor_go/ will generate the following code as ./foo_accessor.go:

// Code generated by gen_foo_accessor_go; DO NOT EDIT.

package examples

import (
	"text/template"

	"github.com/incu6us/goimports-reviser/v3/reviser"
)

func (rcv *Foo) GetBar() string {
	return rcv.bar
}

func (rcv *Foo) SetBar(value string) {
	rcv.bar = value
}

func (rcv *Foo) GetQux() template.Template {
	return rcv.qux
}

func (rcv *Foo) SetQuux(value *reviser.SourceDir) {
	rcv.quux = value
}

Running go run ./gen_foo_accessor_go.go is also feasible if you prefer to place the generator code in the same directory as ./foo.go. Be sure to add //go:build ignore to the top of the generator's code to prevent it from being compiled and linked with other code.

While the above example is using the template AccessorTemplate, you can use your own template by passing it to pinfomap.Generate. The AccessorTemplate is defined as follows:

{{- /* gotype: github.com/knaka/go-pinfomap.Struct */ -}}

// Code generated by {{GeneratorName}}; DO NOT EDIT.

package {{.Package.Name}}

import (
	{{- range .Imports }}
		"{{.}}"
	{{- end }}
)

{{- $structName := .StructName }}

{{- range .PrivateFields }}
	{{- if .Params.getter }}
		func (rcv *{{$structName}}) Get{{.CapName}}() {{.Type}} {
			return rcv.{{.Name}}
		}
	{{- end }}

	{{- if .Params.setter }}
		func (rcv *{{$structName}}) Set{{.CapName}}(value {{.Type}}) {
			rcv.{{.Name}} = value
		}
	{{- end }}
{{- end }}

The resulting Go code is automatically formatted with goimports-reviser, allowing you to write the template without worrying about the strictness of import statements or unnecessary empty lines.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AccessorTemplate string

Functions

func Camel2Snake

func Camel2Snake(sIn string) (s string)

Camel2Snake converts a camel case string to snake case.

func ForceCamel added in v0.0.2024020954

func ForceCamel(sIn string) (s string)

func Plural added in v0.0.3

func Plural(s string) string

func Singular added in v0.0.3

func Singular(s string) string

Types

type Field

type Field struct {
	Name   string
	Type   string
	Tag    string
	Params map[string]any
	Data   any
}

func (*Field) CamelName added in v0.0.2024020954

func (f *Field) CamelName() string

func (*Field) CapName

func (f *Field) CapName() string

func (*Field) LowerCamelName added in v0.0.2024020954

func (f *Field) LowerCamelName() string

func (*Field) SnakeName added in v0.0.3

func (f *Field) SnakeName() string

SnakeName converts a field name to snake case.

type Method

type Method struct {
	Name            string
	Type            string
	PointerReceiver bool
}

type OptSetter added in v0.0.2024020954

type OptSetter func(*ctorParams)

func WithData added in v0.0.2024020954

func WithData(data any) OptSetter

func WithTags added in v0.0.2024020954

func WithTags(tags ...string) OptSetter

type Package

type Package struct {
	Name string
	Path string
	Pkg  *packages.Package
}

func (*Package) GetStringTypes

func (p *Package) GetStringTypes() []string

func (*Package) GetTypes

func (p *Package) GetTypes() []string

type Struct

type Struct struct {
	StructName  string
	Package     *Package
	PackagePath string
	Fields      []*Field
	Methods     []*Method
	Imports     []string
	Data        any
}

func NewStructInfo added in v0.0.2024020954

func NewStructInfo(tgtStruct any, optSetters ...OptSetter) (struct_ *Struct, err error)

func (*Struct) PackageName

func (s *Struct) PackageName() string

func (*Struct) PrivateFields

func (s *Struct) PrivateFields() []*Field

func (*Struct) SnakeStructName

func (s *Struct) SnakeStructName() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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