GithubHelp home page GithubHelp logo

moonject's Introduction

goinject

goinject is a wrapper library for creating Go preprocessors.

Usage

goinject allows you to create custom preprocessors by defining a struct that satisfies the Modifier interface. This interface has only one method, Modify, which accepts a (*dst.File, *decorator.Decorator, *decorator.Restorer) representing the AST of a Go source file, file decorator and imports restorer. This method must return a modified *dst.File.

Process Function

The goinject.Process function represents the generalized approach to preprocessing Go code. It performs the following steps:

  1. Checks if we are at the right stage of compilation.
  2. If not, runs the original command and returns.
  3. Extracts the files that Go is about to compile.
  4. Makes changes to the AST of all the files (without modifying the original source code).
  5. Writes the modified files to a temporary directory.
  6. Resolves all missing imports that were added as part of the modification.
  7. Substitutes the path to the original files with the path to the modified files and passes them to the compiler command.
  8. Runs the original command with the substituted files to be compiled.

Example

Here's an example of how you can use goinject to modify a Go source file:

package main

import (
	"github.com/pijng/goinject"
)

// CustomModifier implements the Modifier interface
type CustomModifier struct{}

// Modify implements the Modify method of the Modifier interface
func (cm CustomModifier) Modify(f *dst.File, dec *decorator.Decorator, res *decorator.Restorer) *dst.File {
	// Add custom modification logic here
	return f
}

func main() {
	// Call goinject.Process with an instance of your modifier struct
	goinject.Process(CustomModifier{})
}

In this example, CustomModifier is a struct that satisfies the Modifier interface. It implements the Modify method, where you can define your custom modification logic.

Demonstration

  • moonjectlog: moonjectlog is a preprocessor that inserts a simple fmt.Println statement at the beginning of each function in a Go project. It demonstrates the usage of goinject for injecting custom logic into source files.
  • go-ifdef: go-ifdef is a preprocessor that allows you to use trivial #ifdef and #else directives based on the GOOS environment variable.

moonject's People

Contributors

pijng avatar

Stargazers

s3rj1k avatar  avatar Dylan avatar  avatar  avatar Aaron McDevitt avatar Illya Marchenko avatar Viktor Nikolaiev avatar  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.