Documentation
¶
Index ¶
- func IsNil(s Snippet) bool
- type Placeholder
- type Snippet
- func Block(v string) Snippet
- func BlockF(v string, args ...any) Snippet
- func BlockRaw(v string) Snippet
- func Comments(lines ...string) Snippet
- func Compose(ss ...Snippet) Snippet
- func Directive(directive string, args ...string) Snippet
- func Document(name string, lines ...string) Snippet
- func Expose(ctx context.Context, path string, name string, targs ...Snippet) Snippet
- func ExposeObject(ctx context.Context, o types.Object, targs ...Snippet) Snippet
- func Ident(ctx context.Context, t typx.Type) Snippet
- func IdentFor[T any](ctx context.Context) Snippet
- func IdentOf[T any](ctx context.Context, v T) Snippet
- func IdentRT(ctx context.Context, t reflect.Type) Snippet
- func IdentTT(ctx context.Context, t types.Type) Snippet
- func Imports(ctx context.Context) Snippet
- func Indent(n int) Snippet
- func InlineComment(line string) Snippet
- func NewLine(n int) Snippet
- func Poster(pkg string, generator string) Snippet
- func Snippets(sep Snippet, ss ...Snippet) Snippet
- func Strings(tail string, sep string, list ...string) Snippet
- func Template(r io.Reader, args ...*TArg) Snippet
- func Value(ctx context.Context, x any) Snippet
- type TArg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Placeholder ¶ added in v0.1.1
type Placeholder struct{}
Placeholder must be not nil and yield an empty string
func (Placeholder) IsNil ¶ added in v0.1.1
func (Placeholder) IsNil() bool
type Snippet ¶
func Block ¶
Example ¶
Print(
context.Background(),
Document("F", "this is a document for F."),
NewLine(1),
Block("func F() string {"),
NewLine(1),
Indent(1), BlockF("x := "), BlockRaw("abc"),
NewLine(1),
Indent(1), Block("return x"),
NewLine(1),
Block("}"), NewLine(1),
)
Output: // F this is a document for F. func F() string { x := "abc" return x }
func Comments ¶
Example ¶
Print(
context.Background(),
Snippets(
NewLine(1),
Document("TypeName", "this is a document for TypeName.", " line1\n line2\n line3"),
Comments("\n", ""), // empty skipped
InlineComment("this is an inline comment"),
Directive("generate", "arg1", "\n", "arg2", "\t", "--flag"),
),
)
Output: // TypeName this is a document for TypeName. // line1 // line2 // line3 // this is an inline comment //go:generate arg1 arg2 --flag
func Expose ¶
Expose create an exposer in some package, it may be a types.Type or a types.Object here the name MUST BE exported case 1: a named/alias type. this case should handled by Ident eg: path/to/package.NamedType[TypeArguments...] case 2: an exported object. the package level MUST be a *types.Func, *types.Const or *types.Var eg: errors.New the underlying is a function: func() error, but we need the object
Example ¶
ctx := dumper.WithTracker(context.Background(), "github.com/xoctopus/genx/testdata")
pkg := dumper.Load(ctx, "github.com/xoctopus/genx/testdata")
t1 := pkg.Types.Scope().Lookup("T").Type().(*types.Named)
t2 := pkg.Types.Scope().Lookup("TT").Type().(*types.Named)
body := Snippets(
NewLine(1),
// imported: project
Expose(ctx, "github.com/xoctopus/genx/testdata", "FuncT", IdentFor[string](ctx)),
Expose(ctx, "github.com/xoctopus/genx/testdata", "DEMO_ENUM_A"),
Expose(ctx, "github.com/xoctopus/genx/testdata", "Var"),
Expose(ctx, "github.com/xoctopus/genx/testdata", "VarFuncT"),
ExposeObject(ctx, t1.Obj(), IdentFor[int](ctx)),
ExposeObject(ctx, t2.Obj(), IdentFor[int](ctx), IdentFor[string](ctx)),
// imported: std
Expose(ctx, "fmt", "Sscanf"),
Expose(ctx, "io", "ReadAll"),
// imported: general
Expose(ctx, "errors", "New"),
Expose(ctx, "github.com/xoctopus/x/ptrx", "Ptr", IdentFor[testdata.DemoEnum](ctx)),
)
Print(
ctx,
Poster("demo", "genx:test_ident"),
Imports(ctx),
body,
)
Output: // Package demo GENERATED BY genx:test_ident DO NOT EDIT package demo import ( "errors" "fmt" "io" "github.com/xoctopus/x/ptrx" ) FuncT[string] DEMO_ENUM_A Var VarFuncT T[int] TT[int, string] fmt.Sscanf io.ReadAll errors.New ptrx.Ptr[DemoEnum]
func ExposeObject ¶
func IdentFor ¶
Example ¶
ctx := dumper.WithTracker(context.Background(), "github.com/xoctopus/genx/testdata")
body := Snippets(
NewLine(1),
IdentFor[string](ctx),
IdentOf(ctx, bytes.Reader{}),
IdentFor[bytes.Reader](ctx),
IdentFor[*bytes.Buffer](ctx),
IdentFor[io.Reader](ctx),
IdentFor[dumper.Import](ctx),
IdentRT(ctx, reflect.TypeOf(3.2)),
IdentTT(ctx, types.Typ[types.Int32]),
)
Print(
ctx,
Poster("demo", "genx:test_ident"),
Imports(ctx),
body,
)
Output: // Package demo GENERATED BY genx:test_ident DO NOT EDIT package demo import ( "bytes" "io" "github.com/xoctopus/genx/internal/dumper" ) string bytes.Reader bytes.Reader *bytes.Buffer io.Reader dumper.Import float64 int32
func InlineComment ¶
func Snippets ¶
Example (Eof) ¶
Print(context.Background(), Compose(Block(eof)))
Print(context.Background(), &Placeholder{})
func Strings ¶ added in v0.1.1
Example ¶
Print(context.Background(), Strings(",", "\n", "1", "2", "3", "4"))
Print(context.Background(), Strings(",", "\n"))
Output: "1", "2", "3", "4",
func Template ¶
Example ¶
ctx := dumper.WithTracker(
context.Background(),
"github.com/xoctopus/genx/testdata",
)
tpl := Template(
bytes.NewReader(template),
ArgFor[testdata.Gender](ctx, "Type"),
ArgExpose(ctx, "fmt", "Sprintf"),
Arg(ctx, "NameToValueCases", Snippets(
NewLine(1),
Compose(Indent(1), Block(`case "MALE":`)),
Compose(Indent(2), Block(`return GENDER__MALE, nil`)),
Compose(Indent(1), Block(`case "FEMALE":`)),
Compose(Indent(2), Block(`return GENDER__FEMALE, nil`)),
)),
ArgExpose(ctx, "github.com/xoctopus/genx/testdata", "GENDER_UNKNOWN").
WithName("UnknownValue"),
ArgExpose(ctx, "errors", "New"),
Arg(ctx, "Values", Snippets(
NewLine(1),
Compose(Indent(2), Expose(ctx, "github.com/xoctopus/genx/testdata", "GENDER__MALE"), Block(",")),
Compose(Indent(2), Expose(ctx, "github.com/xoctopus/genx/testdata", "GENDER__FEMALE"), Block(",")),
)),
ArgExpose(ctx, "github.com/xoctopus/genx/testdata", "Scan").WithName("EnumScanBrick"),
ArgExpose(ctx, "bytes", "ToUpper"),
ArgExpose(ctx, "fmt", "Sscanf"),
ArgT[driver.Value](ctx),
Arg(ctx, "ValueToDescCases", Snippets(
NewLine(1),
Compose(Indent(1), Block(`case GENDER__MALE:`)),
Compose(Indent(2), Block(`return "男"`)),
Compose(Indent(1), Block(`case GENDER__FEMALE:`)),
Compose(Indent(2), Block(`return "女"`)),
)),
Arg(ctx, "ValueToNameCases", Snippets(
NewLine(1),
Compose(Indent(1), Block(`case GENDER__MALE:`)),
Compose(Indent(2), Block(`return "MALE"`)),
Compose(Indent(1), Block("case GENDER__FEMALE:")),
Compose(Indent(2), Block(`return "FEMALE"`)),
)),
nil,
Arg(ctx, "Nil", nil),
)
b := bytes.NewBuffer(nil)
Write(
ctx,
b,
Poster("testdata", "genx:test_template"),
Imports(ctx),
tpl,
)
filename := filepath.Join(must.NoErrorV(os.Getwd()), "..", "..", "testdata", "gender_genx_tpl_test_enum.go")
must.NoError(os.RemoveAll(filename))
output, _ := os.OpenFile(
filename,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0644,
)
_, _ = io.Copy(output, b)
_ = output.Sync()
_ = output.Close()
func Value ¶
Example ¶
ctx := dumper.WithTracker(
context.Background(),
"github.com/xoctopus/genx/pkg/snippet_test",
)
body := Snippets(
NewLine(1),
Value(ctx, 1),
Value(ctx, int8(1)),
Value(ctx, uintptr(100)),
Value(ctx, 1.1),
Value(ctx, float32(1.1)),
Value(ctx, "10"),
Value(ctx, testdata.String("10")),
Value(ctx, func() {}),
Value(ctx, testdata.Gender(1)),
Value(ctx, testdata.Struct{
A: 100,
B: &testdata.Struct{A: 101},
}),
Value(ctx, struct {
X int `json:"x,omitzero"`
}{0}),
Value(ctx, map[int]int{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}),
Value(ctx, map[any]int{
nil: 0,
(*int)(nil): 1,
"1": 2,
"3": 3,
"5": 4,
testdata.String("2"): 5,
testdata.String("4"): 6,
testdata.String("6"): 7,
}),
Value(ctx, []map[any]any{{
testdata.String("0"): 3, // snippet_test.String 3
"1": 2, // string 2
2: 0, // int 0
ptrx.Ptr(testdata.String("3")): 1, // pointer 1
}}),
Value(ctx, []any{nil, true, false, complex64(1), complex128(1)}),
Value(ctx, nil),
)
Print(
ctx,
Poster("demo", "genx:test_value"),
Imports(ctx),
body,
)
Output: // Package demo GENERATED BY genx:test_value DO NOT EDIT package demo import ( "github.com/xoctopus/x/ptrx" "github.com/xoctopus/genx/testdata" ) 1 1 100 1.1 1.1 "10" testdata.String("10") testdata.Gender(1) testdata.Struct{A: 100, B: ptrx.Ptr[testdata.Struct](testdata.Struct{A: 101, B: nil})} struct { X int "json:\"x,omitzero\"" }{X: 0} map[int]int{1: 1, 2: 2, 3: 3, 4: 4, 5: 5} map[interface {}]int{nil: 0, nil: 1, "1": 2, "3": 3, "5": 4, testdata.String("2"): 5, testdata.String("4"): 6, testdata.String("6"): 7} []map[interface {}]interface {}{map[interface {}]interface {}{2: 0, ptrx.Ptr[testdata.String](testdata.String("3")): 1, "1": 2, testdata.String("0"): 3}} []interface {}{nil, true, false, (1+0i), (1+0i)} nil
Click to show internal directories.
Click to hide internal directories.