Knit is an inline code generation tool that combines the power of Go's text/template package with automatic spec file loading.

Knit

Knit is an inline code generation tool that combines the power of Go's text/template package with automatic spec file loading.

Example

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Example Codegen
paths:
  /pets:
    post:
      operationId: GetPet

example.go:

package example

// @knit input $SCHEMA_FILE
// @knit loader openapi
// @knit template ./templates/openapi/test.tmpl

// @!knit

Running knit against this file will first load the schema file using the openapi loader, then execute the template using the data from the loader. The resulting text will then be inserted between the @knit annotations. The annotations are maintained so the code can be generated again when the schema changes.

Here is the resulting code:

package example

// @knit input $SCHEMA_FILE
// @knit loader openapi
// @knit template ./templates/openapi/test.tmpl

type Generated struct {
    Pet string
}

// @!knit

Annotations

Define generators you'd like to knit into your codebase using the @knit annotations.

The parser algorithm first splits the target code file by the ending annotation: @!knit -- It then works backwards on each split block to identify the options for the generator.

Options are defined on the opening annotations in the following format:

@knit 

Annotations also support environment variables. All $env variables will be expanded before the option line is parsed.

Options

Input

The input option specifies the input file. This must be a file on your system.

Relative paths are resolved using the directory in which you've run knit

Loader

The loader option specifies the loader used to load the input file.

Currently the only loader type is openapi

Template

The template option specifies the template file.

Relative paths are resolved using the directory in which you've run knit

Demo

To demo knit, please clone the repository and run the following:

go run cmd/main.go -glob "./test/*.go"
Owner
Tyler
Coding full stack web applications in Go and TypeScript. Car nerd and wannabe racecar driver.
Tyler
Similar Resources

The High Code Framework (low-code for devs)

hof - the high code framework The hof tool tries to remove redundent development activities by using high level designs, code generation, and diff3 wh

Dec 24, 2022

🎄 My code for the Advent of Code of year 2021 in Go.

Advent of Code 2021 This repository contains all code that I wrote for the Advent of Code 2021. This year I chose to try and learn Go. Enjoy! Built wi

Dec 9, 2021

Package tail implements file tailing with fsnotify.

tail Package tail implements file tailing with fsnotify. Fork of nxadm/tail, simplified, reworked and optimized. Currently, supports only Linux and Da

Nov 30, 2022

Go package for tailing file

tail Tail a file programmatically just like you run shell command tail -f /somefile. Features support tail a non-exist file until that file exists tai

Nov 30, 2021

Package fsm allows you to add finite-state machines to your Go code.

fsm Package fsm allows you to add finite-state machines to your Go code. States and Events are defined as int consts: const ( StateFoo fsm.State =

Dec 9, 2022

Enrich `go test` outputs with text decorations.

Enrich `go test` outputs with text decorations.

richgo Rich-Go will enrich go test outputs with text decorations Installation (go get): go get -u github.com/kyoh86/richgo (homebrew): brew tap kyoh8

Dec 28, 2022

A minimalist Go PDF writer in 1982 lines. Draws text, images and shapes. Helps understand the PDF format. Used in production for reports.

A minimalist Go PDF writer in 1982 lines. Draws text, images and shapes. Helps understand the PDF format. Used in production for reports.

one-file-pdf - A minimalist PDF generator in 2K lines and 1 file The main idea behind this project was: "How small can I make a PDF generator for it

Dec 11, 2022

A modern and intuitive terminal-based text editor

A modern and intuitive terminal-based text editor

micro is a terminal-based text editor that aims to be easy to use and intuitive, while also taking advantage of the capabilities of modern terminals.

Jan 5, 2023

The new home of the CUE language! Validate and define text-based and dynamic configuration

The CUE Data Constraint Language Configure, Unify, Execute CUE is an open source data constraint language which aims to simplify tasks involving defin

Dec 31, 2022
Comments
  • CLI

    CLI

    Knit should be usable from the command line. It might be nice to generate standalone files

    Example knit generate --template="template.tmpl" --input="schema.yml" > codegen.go

  • Using backticks in template literals breaks option parser

    Using backticks in template literals breaks option parser

    Currently, the parser regex does not take into account any backticks that may be within the template literal. There should be a way to escape any backticks within a template literal.

    This will break:

    /*
      @knit input ./schema.yml
      @knit loader openapi
      @knit template tmpl`
        type Generated struct {
        {{ range $k, $v := .Paths }} 
            {{ .Post.OperationID }} string `yaml:"{{ .Post.OperationID }}"`
        {{end}}
        }
      `
    */
    

    Expected:

    /*
      @knit input ./schema.yml
      @knit loader openapi
      @knit template tmpl`
        type Generated struct {
        {{ range $k, $v := .Paths }} 
            {{ .Post.OperationID }} string \`yaml:"{{ .Post.OperationID }}"\`
        {{end}}
        }
      `
    */
    
  • IDE Support

    IDE Support

    Adding IDE support for knit

    • Syntax highlighting for knit annotations and template literals
    • Error checking for option types
    • Warnings and checks for inserting hand written code between codegen annotations
    • Execution of knit command from within code hint
Related tags
Works with HashiCorp HCL. Allows to append the input file with blocks and attributes from the template file

About hclmergetool Works with HashiCorp HCL. Allows to append the input file with blocks and attributes from the template file Installation Binary Rel

Feb 6, 2022
Lima launches Linux virtual machines on macOS, with automatic file sharing, port forwarding, and containerd.

Lima: Linux-on-Mac ("macOS subsystem for Linux", "containerd for Mac")

Jan 8, 2023
Set of functions/methods that will ease GO code generation

Set of functions/methods that will ease GO code generation

Dec 1, 2021
Code generation for golang's constructor

constructor Generate constructor for a given struct. Usage of constructor: -exclude string the fields to exclude. Use comma to specify multi

Dec 26, 2021
:sunglasses:Package captcha provides an easy to use, unopinionated API for captcha generation

Package captcha provides an easy to use, unopinionated API for captcha generation. Why another captcha generator? I want a simple and framework-indepe

Dec 28, 2022
Tool to easily rename or move a bunch of files with a text editor of your choice
Tool to easily rename or move a bunch of files with a text editor of your choice

batch-rename With batch-rename you can utilize your favorite text editor to rename or move a bunch of files at once. It doesn't come with any features

Nov 2, 2022
Support CI generation of SBOMs via golang tooling.

Software Package Data Exchange (SPDX) is an open standard for communicating software bill of materials (SBOM) information that supports accurate identification of software components, explicit mapping of relationships between components, and the association of security and licensing information with each component.

Jan 3, 2023
Support CI generation of SBOMs via golang tooling.
Support CI generation of SBOMs via golang tooling.

SPDX Software Bill of Materials (SBOM) Generator Overview Software Package Data Exchange (SPDX) is an open standard for communicating software bill of

Jan 3, 2023
Snowflake algorithm generation worker Id sequence

sequence snowflake algorithm generation worker Id sequence 使用雪花算法生成ID,生成100万个只需要

Jan 21, 2022
Simple Golang API to demonstrate file upload to fireabase storage and retrieving url of uploaded file.

go-firebase-storage -Work in progress ??️ Simple Golang API that uses Firebase as its backend to demonstrate various firebase services using Go such a

Oct 4, 2021