GithubHelp home page GithubHelp logo

isabella232 / goastwriter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from palantir/goastwriter

0.0 0.0 0.0 512 KB

Go library for writing Go source code programatically

License: BSD 3-Clause "New" or "Revised" License

Shell 12.17% Go 87.83%

goastwriter's Introduction

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
}

goastwriter's People

Contributors

bmoylan avatar dzmitrydulko avatar nmiyake avatar svc-autorelease avatar svc-excavator-bot 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.