All Projects → palantir → goastwriter

palantir / goastwriter

Licence: BSD-3-Clause license
Go library for writing Go source code programatically

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to goastwriter

dialogue
A client-side RPC library for conjure-java
Stars: ✭ 12 (-55.56%)
Mutual labels:  octo-correct-managed
rust-zipkin
A library for logging and propagating Zipkin trace information in Rust
Stars: ✭ 50 (+85.19%)
Mutual labels:  octo-correct-managed
asana mailer
A script that uses Asana's RESTful API to generate plaintext and HTML emails.
Stars: ✭ 12 (-55.56%)
Mutual labels:  octo-correct-managed
dropwizard-web-security
A Dropwizard bundle for applying default web security functionality
Stars: ✭ 37 (+37.04%)
Mutual labels:  octo-correct-managed
Tslint
🚦 An extensible linter for the TypeScript language
Stars: ✭ 5,922 (+21833.33%)
Mutual labels:  octo-correct-managed
conjure-rust
Conjure support for Rust
Stars: ✭ 14 (-48.15%)
Mutual labels:  octo-correct-managed
go-license
Go tool that applies and verifies that proper license headers are applied to Go files
Stars: ✭ 42 (+55.56%)
Mutual labels:  octo-correct-managed
go-baseapp
A lightweight starting point for Go web servers
Stars: ✭ 61 (+125.93%)
Mutual labels:  octo-correct-managed
Python Language Server
An implementation of the Language Server Protocol for Python
Stars: ✭ 2,412 (+8833.33%)
Mutual labels:  octo-correct-managed
phishcatch
A browser extension and API server for detecting corporate password use on external websites
Stars: ✭ 75 (+177.78%)
Mutual labels:  octo-correct-managed
gradle-docker-test-runner
Gradle plugin for running tests in Docker environments
Stars: ✭ 20 (-25.93%)
Mutual labels:  octo-correct-managed
Blueprint
A React-based UI toolkit for the web
Stars: ✭ 18,376 (+67959.26%)
Mutual labels:  octo-correct-managed
palantir-java-format
A modern, lambda-friendly, 120 character Java formatter.
Stars: ✭ 203 (+651.85%)
Mutual labels:  octo-correct-managed
log4j-sniffer
A tool that scans archives to check for vulnerable log4j versions
Stars: ✭ 180 (+566.67%)
Mutual labels:  octo-correct-managed
giraffe
Gracefully Integrated Remote Access For Files and Execution
Stars: ✭ 50 (+85.19%)
Mutual labels:  octo-correct-managed
gradle-npm-run-plugin
No description or website provided.
Stars: ✭ 19 (-29.63%)
Mutual labels:  octo-correct-managed
amalgomate
Go tool for combining multiple different main packages into a single program or library
Stars: ✭ 19 (-29.63%)
Mutual labels:  octo-correct-managed
hadoop-crypto
Library for per-file client-side encyption in Hadoop FileSystems such as HDFS or S3.
Stars: ✭ 38 (+40.74%)
Mutual labels:  octo-correct-managed
witchcraft-go-server
A highly opinionated Go embedded application server for RESTy APIs
Stars: ✭ 47 (+74.07%)
Mutual labels:  octo-correct-managed
gradle-consistent-versions
Compact, constraint-friendly lockfiles for your dependencies
Stars: ✭ 92 (+240.74%)
Mutual labels:  octo-correct-managed

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
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].