internal

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	//go:embed version.txt
	Version string
	//go:embed build.txt
	BuildTime string
)

nolint

Functions

func LoadPackages

func LoadPackages(
	ctx context.Context,
	pwd string,
	env []string,
	tags string,
	patterns []string,
) ([]*packages.Package, []error)

LoadPackages typechecks the packages that match the given patterns and includes source for all transitive dependencies. The patterns are defined by the underlying build system. For the go tool, this is described at https://golang.ir/cmd/go/#hdr-Package_lists_and_patterns

wd is the working directory and env is the set of environment variables to use when loading the packages specified by patterns. If env is nil or empty, it is interpreted as an empty set of variables. In case of duplicate environment variables, the last one in the list takes precedence.

Types

type Bind

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

func NewBind

func NewBind() *Bind

func (*Bind) Add

func (p *Bind) Add(path string)
Example
package main

import (
	"gitee.com/xuender/bindgen/internal"
)

func main() {
	bind := internal.NewBind()
	bind.Add("../_example/model")

}
Output:

func (*Bind) Bind

func (p *Bind) Bind(igen *bindGen, target, source *types.Var)

func (*Bind) Geter

func (p *Bind) Geter(pkgName, model string) Fielders

func (*Bind) Seter

func (p *Bind) Seter(pkgName, model string) Fielders

type BindArgs

type BindArgs struct {
	// Name is the name of the injector function.
	Name string
	// Tuple represents the arguments.
	Tuple *types.Tuple
	// Pos is the source position of the injector function.
	Pos token.Pos
}

BindArgs describes the arguments passed to an injector function.

type FieldMod

type FieldMod int
const (
	Exported FieldMod = iota
	GetSet
	Get
	Set
)

type Fielder

type Fielder struct {
	Name    string
	Mod     FieldMod
	Type    string
	Imports map[string]ImportInfo
}

func (*Fielder) Val added in v0.0.3

func (p *Fielder) Val(sourceName, targetFieldType string) string
Example
package main

import (
	"fmt"

	"gitee.com/xuender/bindgen/internal"
)

func main() {
	field := internal.Fielder{
		Name:    "FieldName",
		Mod:     internal.Get,
		Type:    "int",
		Imports: map[string]internal.ImportInfo{},
	}

	fmt.Println(field.Val("source", "int32"))

	field.Mod = internal.Exported
	fmt.Println(field.Val("source", "float64"))

	field.Type = "bool"
	fmt.Println(field.Val("source", "int32"))

	field.Type = "int"
	fmt.Println(field.Val("source", "string"))

	field.Type = "float32"
	fmt.Println(field.Val("source", "string"))

	field.Type = "string"
	fmt.Println(field.Val("source", "int"))

	field.Type = "string"
	fmt.Println(field.Val("source", "int64"))

	field.Type = "string"
	fmt.Println(field.Val("source", "float64"))

	field.Type = "string"
	fmt.Println(field.Val("source", "float32"))

}
Output:

int32(source.GetFieldName())
float64(source.FieldName)
conv.IfElse[int32](source.FieldName, 1, 0)
strconv.Itoa(source.FieldName)
strconv.FormatFloat(float64(source.FieldName), 'f', -1, 32)
conv.Must(strconv.Atoi(source.FieldName))
conv.Must(strconv.Atoi(int(source.FieldName)))
conv.Must(strconv.ParseFloat(source.FieldName, 64))
float32(conv.Must(strconv.ParseFloat(source.FieldName, 32)))

type Fielders

type Fielders []Fielder

func (*Fielders) Add

func (p *Fielders) Add(fielder Fielder)

func (Fielders) Find

func (p Fielders) Find(name string) Fielder

func (Fielders) Mixed

func (p Fielders) Mixed(other Fielders) []string

type Gen

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

Gen is the file-bindgen generator state.

func NewGen

func NewGen(pkg *packages.Package) *Gen

func (*Gen) Frame

func (g *Gen) Frame(tags string) []byte

Frame bakes the built up source body into an unformatted Go source file.

func (*Gen) Pf

func (g *Gen) Pf(format string, args ...any)

type GenerateOptions

type GenerateOptions struct {
	// Header will be inserted at the start of each generated file.
	Header           []byte
	PrefixOutputFile string
	Tags             string
}

GenerateOptions holds options for Generate.

type GenerateResult

type GenerateResult struct {
	// PkgPath is the package's PkgPath.
	PkgPath string
	// OutputPath is the path where the generated output should be written.
	// May be empty if there were errors.
	OutputPath string
	// Content is the gofmt'd source code that was generated. May be nil if
	// there were errors during generation.
	Content []byte
	// Errs is a slice of errors identified during generation.
	Errs []error
}

GenerateResult stores the result for a package from a call to Generate.

func Generate

func Generate(
	ctx context.Context,
	pwd string,
	env []string,
	patterns []string,
	opts *GenerateOptions,
) ([]GenerateResult, []error)

Generate may return one or more errors if it failed to load the packages.

func (GenerateResult) Commit

func (gen GenerateResult) Commit() error

Commit writes the generated file to disk.

type ImportInfo added in v0.0.3

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

ImportInfo holds info about an import.

Jump to

Keyboard shortcuts

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