goastwriter

package module
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: BSD-3-Clause Imports: 7 Imported by: 4

README

Autorelease

goastwriter 👻✍️

goastwriter is a library that offers abstractions for defining and writing Go source files programmatically. It is effectively a convenience wrapper for the structs and functions defined in the go/ast package. However, by providing higher-level abstractions, convenience functions and tests, goastwriter makes writing Go code programatically much simpler for simple use cases.

goastwriter processes its generated code using gofmt, so its output is gofmt-compliant.

Usage

goastwriter.Write is the primary function exported by the package and generates the code for a single Go file. It is provided with the package name that should be used for the file and the components that make up the file.

Example

Code for generating a Go source file:

out, _ := goastwriter.Write("testpkg",
    decl.NewImports(map[string]string{
        "fmt":       "",
        "go/format": "gofmt",
    }),
    &decl.Struct{
        Name:    "Foo",
        Comment: "Foo is a struct",
        Fields: []decl.StructField{
            {
                Name:    "Bar",
                Type:    expression.StringType,
                Comment: "Bar is a field",
            },
            {
                Name:    "baz",
                Type:    expression.BoolType.Pointer(),
                Comment: "Baz is a field",
            },
        },
    },
    &decl.Function{
        Name: "Bar",
        Params: []*decl.FuncParam{
            decl.NewFuncParam("input", expression.Type("Foo").Pointer()),
        },
        ReturnTypes: []expression.Type{
            expression.Type("Foo").Pointer(),
            expression.ErrorType,
        },
        Body: []astgen.ASTStmt{
            &statement.Expression{
                Expr: expression.NewCallFunction("fmt", "Println"),
            },
            &statement.Expression{
                Expr: expression.NewCallFunction("gofmt", "Source", expression.Nil),
            },
            &statement.Return{
                Values: []astgen.ASTExpr{
                    expression.VariableVal("input"),
                    expression.Nil,
                },
            },
        },
    },
)
fmt.Println(string(out))

Output:

package testpkg

import (
	"fmt"
	gofmt "go/format"
)

// Foo is a struct
type Foo struct {
	// Bar is a field
	Bar string
	// Baz is a field
	baz *bool
}

func Bar(input *Foo) (*Foo, error) {
	fmt.Println()
	gofmt.Source(nil)
	return input, nil
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(pkgName string, components ...astgen.ASTDecl) ([]byte, error)
Example
package main

import (
	"fmt"

	"github.com/palantir/goastwriter"
	"github.com/palantir/goastwriter/astgen"
	"github.com/palantir/goastwriter/decl"
	"github.com/palantir/goastwriter/expression"
	"github.com/palantir/goastwriter/statement"
)

func main() {
	out, _ := goastwriter.Write("testpkg",
		decl.NewImports(map[string]string{
			"fmt":       "",
			"go/format": "gofmt",
		}),
		&decl.Struct{
			Name:    "Foo",
			Comment: "Foo is a struct",
			StructType: expression.StructType{
				Fields: []*expression.StructField{
					{
						Name:    "Bar",
						Type:    expression.StringType,
						Comment: "Bar is a field",
					},
					{
						Name:    "baz",
						Type:    expression.BoolType.Pointer(),
						Comment: "Baz is a field",
					},
				},
			},
		},
		&decl.Function{
			Name: "Bar",
			FuncType: expression.FuncType{
				Params: []*expression.FuncParam{
					expression.NewFuncParam("input", expression.Type("Foo").Pointer()),
				},
				ReturnTypes: []expression.Type{
					expression.Type("Foo").Pointer(),
					expression.ErrorType,
				},
			},
			Body: []astgen.ASTStmt{
				&statement.Expression{
					Expr: expression.NewCallFunction("fmt", "Println"),
				},
				&statement.Expression{
					Expr: expression.NewCallFunction("gofmt", "Source", expression.Nil),
				},
				&statement.Return{
					Values: []astgen.ASTExpr{
						expression.VariableVal("input"),
						expression.Nil,
					},
				},
			},
		},
	)
	fmt.Println(string(out))
}
Output:

package testpkg

import (
	"fmt"
	gofmt "go/format"
)

// Foo is a struct
type Foo struct {
	// Bar is a field
	Bar string
	// Baz is a field
	baz *bool
}

func Bar(input *Foo) (*Foo, error) {
	fmt.Println()
	gofmt.Source(nil)
	return input, nil
}

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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