Git Product home page Git Product logo

jot's Introduction

jot

jot is a simple and incomplete API for programmatically generating Go code.

jot.File("hello").
    AddStruct(jot.Struct("HelloPrinter").
        AddField(Field("hello", Type(reflect.TypeOf((*string)(nil)).Elem())).SetDoc("This is a field.")).
        // We could also init HelloPrinters spec outside of this chain and use
        // Struct.Type() to get the type.
        AddMethodRecv("r", true, jot.Function("Print").
            AddParameter("who", jot.Type(reflect.TypeOf((*string)(nil)).Elem())).
            AddReturnType(jot.Type(reflect.TypeOf((*string)(nil)).Elem())).
            AddCodeFmt(`return fmt.Sprintf("%s %s\n", r.hello, {0})`, "who"))).
    Generate(os.Stdout)

which generates:

package hello

import "fmt"

type HelloPrinter struct{
	// This is a field.
	hello string
}

func (r *HelloPrinter) Print(who string) string {
	return fmt.Sprintf("%s %s\n", r.hello, who)
}

jot also allows the user to plug in their own generators that satisfy the Spec interface:

type GenerateSpec struct {
    Cmd []interface{}
}

func (s *GenerateSpec) Write(w io.Writer) error {
    io.WriteString(w, fmt.Sprintf("//go:generate %s", s.Cmd...))
}

func Generate(args ...interface{}) Spec {
    return &GenerateSpec{Cmd: args}
}

jot.File("plugin_example").
	AddSpec(Generate("ls")).
	Generate(os.Stdout)

which generates:

package plugin_example

//go:generate ls

jot's People

Contributors

dforsyth avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.