knit

module
v0.8.0-beta Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2022 License: MIT

README

Knit

knit is a code generation toolkit that simplifies the process of adding and maintaining custom code generators in any project. The tool is written in Go, but its usage is not limited to just Go projects. Generate code into any file using just an input and a template. Inputs can be in a variety of formats including plain json or yaml, or use custom loaders to parse openapi and graphql files.

Installation

Homebrew

Users with homebrew can simply install via tap:

brew install knitcodegen/tap/knit
Go Install

If you're working in a Go environment

go install github.com/knitcodegen/knit/cmd/knit@latest

Examples

The following examples will use knit to generate code from an openapi 3.0 specification.

openapi.yml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Example Codegen
paths:
  /pets:
    post:
      operationId: CreatePet
  /cars:
    post:
      operationId: CreateCar

template.tmpl

type Server interface {
{{ range $k, $v := .Paths -}} 
  {{ .Post.OperationID }}(w http.ResponseWriter, r *http.Request)
{{end}}
}

example.go:

package example

/*
  @knit input ./openapi.yml
  @knit loader openapi3
  @knit template ./template.tmpl 
*/
// @+knit
// @!knit

Now running knit example.go will update the file:

package example

/*
  @knit input ./openapi.yml
  @knit loader openapi3
  @knit template ./template.tmpl 
*/
// @+knit

type Server interface {
  CreatePet(w http.ResponseWriter, r *http.Request)
  CreateCar(w http.ResponseWriter, r *http.Request)
}

// @!knit

The same code can also be generated from the command line. Instead of inserting the result into a file, it's written to stdout:

knit generate \
  --input="./openapi.yml" \
  --loader="openapi3" \
  --template="./template.tmpl" > codegen.go

codegen.go:

type Server interface {
  CreatePet(w http.ResponseWriter, r *http.Request)
  CreateCar(w http.ResponseWriter, r *http.Request)
}

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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