GithubHelp home page GithubHelp logo

knaka / go-pinfomap Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 125 KB

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

License: Apache License 2.0

Go 100.00%

go-pinfomap's Introduction

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.

go-pinfomap's People

Contributors

knaka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.