reflect2go

package module
v0.0.0-...-9a12798 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: MIT Imports: 8 Imported by: 0

README

package reflect2go

GoDoc Go Report Card Build Status Coverage Status

Package reflect2go generates the Go code for a given reflect.Type.

This is useful when you want to generate the Go code for a dynamically defined type, like with reflect.StructOf.

An example use case is a program that updates or manipulates the tags on the fields of an existing struct.

Documentation

Overview

Package reflect2go generates the Go code for a given reflect.Type.

This is useful when you want to generate the Go code for a dynamically defined type, like with reflect.StructOf.

An example use case is a program that updates or manipulates the tags on the fields of an existing struct.

Index

Examples

Constants

View Source
const DefaultCommentLineWrap = 79

DefaultCommentLineWrap is the default CommentLineWrap limit used to wrap all comments.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoFile

type GoFile struct {
	// contains filtered or unexported fields
}

GoFile represents and renders a file with Golang code.

Example
package main

import (
	"fmt"
	"os"
	"reflect"
	"strings"

	"github.com/AdamSLevy/reflect2go"
)

func main() {

	// We will generate a new type based on this predefined Original type.
	// The updated type will include an additional `env` struct field tag
	// based on the existing `json` tag.
	type Original struct {
		A int `json:"a"`
		B int `json:"b"`
	}

	typ := reflect.TypeOf(Original{})
	var fields []reflect.StructField
	for i := 0; i < typ.NumField(); i++ {
		sf := typ.Field(i)

		val := strings.ToUpper(sf.Tag.Get("json"))
		sf.Tag += reflect.StructTag(fmt.Sprintf(" env:%q", val))

		fields = append(fields, sf)
	}

	newTyp := reflect.StructOf(fields)

	gofile, err := reflect2go.NewGoFile("pkg", reflect2go.DefineType("UpdatedTags", newTyp))
	if err != nil {
		panic(err)
	}

	if err := gofile.Render(os.Stdout); err != nil {
		panic(err)
	}

}
Output:

package pkg

type UpdatedTags struct {
	A int `json:"a" env:"A"`
	B int `json:"b" env:"B"`
}

func Must

func Must(gf GoFile, err error) GoFile

Must returns the GoFile but panics if err is not nil.

err := Must(NewGoFile("pkg",
        DefineType("X", reflect.TypeOf(struct{}{})),
        DefineType("Y", reflect.TypeOf(struct{}{}), Comment("Y is a type.")),
)).Render(os.Stdout)

func NewGoFile

func NewGoFile(pkgPath string, opts ...Option) (GoFile, error)

NewGoFile returns a GoFile that belongs to the package with import path pkgPath.

The pkgPath is used to identify reflected types that belong to the same package, and thus don't require a package prefix in their declaration (e.g. pkg.Type vs Type).

The file's package declaration will use path.Base(pkgPath) by default. Use the Option PkgName to override this.

All comments are word wrapped at the limit set by CommentLineWrap, which defaults to DefaultCommentLineWrap.

The order of Options has no significance or affect on the output. Comments are wrapped and defined types are sorted alphabetically prior to output.

func (*GoFile) DefineType

func (f *GoFile) DefineType(name string, typ reflect.Type, opts ...TypeOption) error

DefineType adds a new type declaration to the GoFile. An error is returned if the name has already been used on a type declaration within the GoFile.

func (GoFile) Render

func (f GoFile) Render(w io.Writer) error

Render writes the GoFile to w.

type Option

type Option func(*GoFile) error

Option is an optional setting for NewGoFile.

func CommentLineWrap

func CommentLineWrap(limit int) Option

CommentLineWrap sets the limit used to wrap all comments on word boundaries.

func DefineType

func DefineType(name string, typ reflect.Type, opts ...TypeOption) Option

DefineType defines a type on the NewGoFile.

It is equivalent to calling GoFile.DefineType on an existing GoFile.

func PkgComment

func PkgComment(cmnt string) Option

PkgComment sets a top level package comment which immediately precedes the package declaration.

func PkgName

func PkgName(name string) Option

PkgName overrides the package name, which by default is derived from the path.Base of the package path passed to NewGoFile.

func PreambleComment

func PreambleComment(cmnt string) Option

PreambleComment adds a top level comment that precedes all other content in the rendered GoFile. A blank line is added immediately after the comment to avoid it being confused with a package comment.

type TypeOption

type TypeOption func(*typeDef)

TypeOption is an option for DefineType.

func Comment

func Comment(cmt string) TypeOption

Comment adds a top level comment to a defined type.

Jump to

Keyboard shortcuts

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