Code Monkey home page Code Monkey logo

golangannotations's Introduction

Build Status Coverage Status BCH compliance Maintainability

Detailed explanation

Summary

The golangAnnotations-tool parses your golang source-code into an intermediate representation.

Using this intermediate representation, the tool uses your annotations to generate source code that would be cumbersome and error-prone to write manually.

Bottom line, a lot less code needs to be written.

Example:

// @RestOperation( method = "GET", path = "/person/{uid}" )
func (s *Service) getPerson(c context.Context, uid string) (*Person, error) {
    ...
} 

Based on the annotation line code is generated that will do do all http handling:

  • read-request
  • unmarshall request
  • call business logic
  • marshall response
  • write response

In addition, typestrong test functions are generated that ease testing of your rest operations.

The same "annotation"-approach is used to ease event-sourcing.

Getting the software

$ go get -u -t -v github.com/MarcGrol/golangAnnotations/...

Testing and installing

$ make gen
$ make test
$ make install

or

$ make

Currently supported annotations

This first implementation provides the following kind of annotations:

  • web-services (jax-rs like):

    • Generate server-side http-handling for a "service"
    • Generate client-side http-handling for a "service"
    • Generate helpers to ease integration testing of your services
  • event-listeners:

    • Generate server-side http-handling for receiving events
    • Generate helpers to ease integration testing of your event-listeners
  • event-sourcing:

    • Describe which events belong to which aggregate
    • Type-strong boiler-plate code to build an aggregate from individual events
    • Type-strong boiler-plate code to wrap and unwrap events into an envelope so that it can be easily stored and emitted

How to use http-server related annotations ("jax-rs"-like)?

A regular golang struct definition with our own "RestService" and "RestOperation"-annotations. Observe that ./examples/rest/tourService.go is used as input.

// @RestService( path = "/api" )
type Service struct {
   ...
}

// @RestOperation( method = "GET", path = "/person/{uid}" )
func (s *Service) getPerson(c context.Context, uid string) (*Person, error) {
    ...
}        

Observe that ./examples/rest/gen_tourService.go have been generated.

Example of the generated http handler.

How to use event-sourcing related annotations?

A regular golang struct definition with our own "Event"-annotation.

// @Event( aggregate = Tour" )
type TourEtappeCreated struct {
    ...
}        

Observe that ./examples/event/gen_wrappers.go and ./examples/event/gen_aggregates.go have been created in ./examples/structExample.

Command to trigger code-generation:

We use the "go:generate" mechanism to trigger our goAnnotations-executable. In order to trigger this mechanisme we use a '//go:genarate' comment with the command to be executed.

example:

//go:generate golangAnnotations -input-dir .

So can can use the regular toolchain to trigger code-genaration

$ cd ${GOPATH/src/github.com/MarcGrol/golangAnnotations
$ go generate ./...
// go imports will fix all the imports
$ for i in `find . -name "*.go"`; do goimports -w -local github.com/ ${i}; done
// fixes formatting for generated code
$ for i in `find . -name "*.go"`; do gofmt -s -w ${i}; done

golangannotations's People

Contributors

henkerik avatar ldej avatar marcgrol avatar mmartina avatar raboof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

golangannotations's Issues

Function argument comments are empty

Hey team,
I'm using annotationParser.ParseSourceDir() to parse a file into a model. I've noticed that function argument comments (model.Field.DocLines and model.Field.CommentsLines) are always empty. I've done some debugging and saw that ast model function argument comments are empty too.
This is a known issue: golang/go#35484
They suggest using ast.NewCommentMap() to load the function argument comments: https://go.dev/play/p/zDcLxXqTk0
Thanks!

update problem

when file gen-ast.json is existed

i try modify xxx.go some annotation

image

and command go generate

but gen-ast.json is also

image

Unable to see generated code in Github

Hi there,

Love the idea. The README points to generated examples, but they are 404 on Github. Mind checking in those links so that people can see? (or fix the links)

Production Readiness?

First of all thanks a million for taking the time to write this awesome go library. I wanted to ask if this is production ready.

Go module support

Does this actually support go modules? I tried to generate code on a small POC to try this out but ran into the error below when I ran golangAnnotations -input-dir . in directory containing my service.go file:

Error generating module event: Code /Users/username/code/cmd/accounts/service lives outside GOPATH:/Users/username/go

Typescript-lib web client generation

This is more of a feature request or an open question about whether or not a client lib generator for typescript exists or is planned; similar to what is provided by https://github.com/webrpc/webrpc. My preference to this project over webrpc is that it is a code-first approach rather than a schema-first approach. However, it appears that the developer must create the client-side web library in order to communicate with the backend smoothly.

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.