Code Monkey home page Code Monkey logo

fxforce5's Introduction

FxForce5

Attempt to use AST-based code rewriting to rewrite code for using Uber FX. Starting out with a particular use case.

Behavior

This expects a module that has:

  1. Some struct such as
type X struct {
 Field1 Type1
 Field2 Type2
 ...
}
  1. And a constructor NewX such as:
func NewX(Field1 Type1, Field2 Type2, ...) X

or

func NewX(Field1 Type1, Field2 Type2, ...) *X

(The return type can either be a value X or pointer to it *X).

The behavior is to walk through the code and rewrite it as follows:

  1. Add an XParams struct:
type X struct {
 fx.In
 
 Field1 Type1
 Field2 Type2
 ...
}

(For why the duplication of the fields is needed instead of just embedding X in XParams, see discussion at uber-go/fx#1110.

  1. Replace NewX constructor with NewXOrig.

  2. Add a new constructor NewX such as:

func NewX(params XParams) X {
  diutils.ConstructVal[X, XParams](params)
}

or

func NewX(params XParams) *X {
  diutils.Construct[X, XParams](params)
}

The diutils.Construct() or diutils.ConstructVal() uses reflection to properly assign fields.

  1. Add, if needed, appropriate imports: go.uber.org/fx and/or github.com/debedb/fxforce5/diutils.

  2. Add, if needed, the above dependencies into go.mod (TODO).

  3. Add an fx.Module var:

var X = fx.Module("X", fx.Provide(NewX))

Known issues

See also

fxforce5's People

Contributors

debedb avatar

Watchers

 avatar

fxforce5's Issues

Add CLI flags

  1. Ignore directory patterns
  2. Whether to replace files or write them out with _new suffix (default)

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.