codegen

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

codegen - Utilities for Go code generators

GoDoc Travis-CI Go Report Card

Utilities for writing code generators from a text template, with output properly formatted thanks to gofmt.

See documentation and example.

License

Copyright 2016-2017 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package codegen provides utilities for writing short Go code generators from a text/template.

Example
package main

import (
	"github.com/dolmen-go/codegen"
	"log"
	"os"
)

func main() {
	const template = `// Code generated by example_test.go; DO NOT EDIT.
{{/**/}}//+build {{.}}

package main

import "os"

func main() {
	os.Stdout.WriteString("Hello, {{.}}!\n")
}
`

	tmpl := codegen.MustParse(template)
	for _, tag := range os.Args[1:] {
		f := "main_" + tag + ".go"
		if err := tmpl.CreateFile(f, tag); err != nil {
			log.Fatal(err)
		}
		log.Printf("File %s created.\n", f)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var GeneratedCodeRegexp = regexp.MustCompile(`(?m:^// Code generated .* DO NOT EDIT\.$)`)

GeneratedCodeRegexp checks the code generation standard defined at https://golang.org/s/generatedcode.

Functions

func CreateFile

func CreateFile(filePath string, t *template.Template, data interface{}) (err error)

CreateFile runs the "text/template".Template with data, pass it through gofmt and saves it to filePath.

Types

type CodeTemplate

type CodeTemplate struct {
	Template *template.Template // See "text/template"
	Buffer   bytes.Buffer       // Used for sharing allocated memory between multiple CreateFile calls
	// contains filtered or unexported fields
}

CodeTemplate is the precompiled template for generating one or multiple Go source files.

func MustParse

func MustParse(codeTemplate string) *CodeTemplate

MustParse wraps Parse throwing errors as exception.

func Parse

func Parse(codeTemplate string) (*CodeTemplate, error)

Parse creates a CodeTemplate from a "text/template" source.

The expansion of the template is expected to be valid a Go source file containing the code generation standard tag. See GeneratedCodeRegexp.

func (*CodeTemplate) CreateFile

func (t *CodeTemplate) CreateFile(filePath string, data interface{}) error

CreateFile runs the template with data, pass it through gofmt and saves it to filePath.

The code generation standard at https://golang.org/s/generatedcode is enforced.

Jump to

Keyboard shortcuts

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