Coder Social home page Coder Social logo

genutils's Introduction

genutils

Utilities for writing Go code generators.

Getting Started

The genutils command line tool is used to generate code for writing generators. Run the following command to initialize a generator command line tool, that will be used to generate code using 2 markers.

go run github.com/alexandremahdhaoui/genutils/cmd/genutils@latest --cmd gencmd --generators="yourgen:./pkg/gen,anothergen:./pkg/gen"

Cmd

// cmd/yourgen/main.go
package main

import (
	genutils "github.com/alexandremahdhaoui/genutils"
	gen "github.com/alexandremahdhaoui/genutils/pkg/gen"
)

const (
	name                    = "gencmd"
	description             = "TODO: Please write a description here."
	helper                  = "TODO: Please write an example here."
	yourgenGeneratorName    = "yourgen"
	anothergenGeneratorName = "anothergen"
)

func main() {
	genutils.
		New(name).
		WithDescription(description).
		WithHelper(helper).
		WithGenerator(yourgenGeneratorName, gen.YourgenGenerator{}).
		WithGenerator(anothergenGeneratorName, gen.AnothergenGenerator{}).
		Apply().
		Run()
}

Generator

// pkg/yourgen/yourgen.go
package gen

import (
	genall "sigs.k8s.io/controller-tools/pkg/genall"
	markers "sigs.k8s.io/controller-tools/pkg/markers"
)

var yourgenMarkerDefinition = markers.Must(markers.MakeDefinition("gencmd:yourgen", markers.DescribesType, YourgenGenerator{}))

type YourgenGenerator struct {
	HeaderFile string `marker:",omitempty"`
	Year       string `marker:",omitempty"`
}

func (YourgenGenerator) RegisterMarkers(into *markers.Registry) error {
	if err := markers.RegisterAll(into, yourgenMarkerDefinition); err != nil {
		return err
	}
	into.AddHelp(yourgenMarkerDefinition, markers.SimpleHelp("object", ""))
	return nil
}

func (g YourgenGenerator) Generate(ctx *genall.GenerationContext) error {
	// TODO: ADD YOUR CODE HERE
	for _, root := range ctx.Roots {
		root.NeedTypesInfo()
		markerSet, err := markers.PackageMarkers(ctx.Collector, root)
		if err != nil {
			return err
		}
		markerValues := markerSet[yourgenMarkerDefinition.Name]
		if len(markerValues) == 0 {
			continue
		}
		// TODO: YOU CAN ALSO ADD YOUR CODE HERE
	}
	// TODO: OR HERE
	return nil
}

Use your new generator

Use it on a struct

//+gencmd:yourgen

type YourStruct struct{}

Execute your generator

//go:generate go run github.com/username/repo/cmd/gencmd@latest

package yourpkg

genutils's People

Contributors

alexandremahdhaoui avatar semantic-release-bot avatar

Watchers

 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.