Git Product home page Git Product logo

glue's Introduction

Glue

Note
Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.

Glue generates client code for your Go RPC server. It currently supports

Installation

go get github.com/segmentio/glue/cmd/glue

Then, glue should be available at $GOPATH/bin/glue (ideally, in your $PATH).

Usage

glue -name=Service -service=Math [path] will traverse the provided path (or working directory if none is provided) and generate clients for RPC methods (pointed at Math.*) declared on type Service.

Given the following is in a *.go file in your working directory,

package math

//go:generate glue -name Service -service Math
type Service struct{}

type SumArg struct {
	Values []int
}

type SumReply struct {
	Sum int
}

func (s *Service) Sum(arg SumArg, reply *SumReply) error {
	for _, v := range arg.Values {
		reply.Sum += v
	}

	return nil
}

go generate would output the following to clients/Service.go

package client

import (
	"github.com/segmentio/glue/example/stl/math"
)

type Client interface {
	Call(method string, args interface{}, reply interface{}) error
}

func NewMathClient(rpcClient Client) *Math {
	c := new(Math)
	c.RPC = rpcClient
	return c
}

type Math struct {
	RPC Client
}

func (c *Math) Sum(args math.SumArg) (*math.SumReply, error) {
	reply := new(math.SumReply)
	err := c.RPC.Call("Math.Sum", args, reply)
	return reply, err
}

Gorilla

If you use gorilla/rpc, you're in luck! Just specify -gorilla.

Options

Output

Glue always outputs code with client package. By default, this is in ./client, but you can change the output directory via -out.

To output code to STDOUT instead of files, supply -print.

FAQ

How do I use Glue with RPC implementation X?

Glue is modular. If you'd like support for another popular (or interesting, well-maintained) RPC implementation, open a PR to add a new Glue provider/.

Unfortunately, Go doesn't allow dynamic loading of packages so if you'd like Glue to support an internal or experimental RPC framework, fork Glue and supply another provider in cmd/glue/main.go.

glue's People

Contributors

bhavanki avatar dominicbarnes avatar maxence-charriere avatar tejasmanohar 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

Watchers

 avatar  avatar  avatar  avatar

glue's Issues

Maps of primitives should be treated like basic types

Glue should only allow exported OR builtin types in req/res of RPC methods, like Gorilla and stdlib net/rpc do. It seems like we're failing to detect some containers of basic types as builtin types... may just apply to maps, as we have []int working in examples/.

func (s *Service) MapOfPrimitives(r *http.Request, arg map[string]string, reply *[]int) error {
	*reply = []int{1, 2, 3}
	fmt.Println(arg)
	return nil
}

With -debug,

skipping MapOfPrimitives: argument parameter's type map[string]string is not exported

Panic on empty interface{} type

We can't support interfaces with actual methods since they're not encodable obviously... but we should support the empty interface{} type (Go's "any" value).

panic: unexpected type: *types.Interface

goroutine 1012 [running]:
github.com/segmentio/glue/provider/internal.GetTypeInfo(0x148f880, 0xc4326e8e10, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/provider/internal/types.go:98 +0x4d7
github.com/segmentio/glue/provider/internal.GetTypeInfo(0x148f8c0, 0xc4259002c0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/provider/internal/types.go:91 +0x566
github.com/segmentio/glue/provider/internal.GetTypeInfo(0x148f940, 0xc432665fd0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/provider/internal/types.go:66 +0xd15
github.com/segmentio/glue/provider/stl.(*Provider).GetArgType(0x14c8b00, 0xc431be5310, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/provider/stl/stl.go:67 +0xac
github.com/segmentio/glue/provider/gorilla.(*Provider).GetArgType(0xc420012a80, 0xc4326e81e0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/provider/gorilla/gorilla.go:31 +0x95
github.com/segmentio/glue.(*Generator).Generate(0xc42a173de0, 0x12e574b, 0x6, 0x7fff5fbff99c, 0x9, 0xc429fc5880, 0xa, 0x10, 0x0, 0x0, ...)
	/Users/tejas/dev/src/github.com/segmentio/glue/generator.go:64 +0x171
github.com/segmentio/glue.(*Walker).walkPackage(0xc42000a840, 0xc420084840, 0x7fff5fbff984, 0xe, 0x7fff5fbff99c, 0x9, 0x0, 0x0)
	/Users/tejas/dev/src/github.com/segmentio/glue/walker.go:79 +0x543
github.com/segmentio/glue.(*Walker).Walk.func1(0xc431f1e9e0, 0xc42000a840, 0x12e3bd5, 0x1, 0x7fff5fbff984, 0xe, 0x7fff5fbff99c, 0x9, 0xc420084840)
	/Users/tejas/dev/src/github.com/segmentio/glue/walker.go:48 +0x90
created by github.com/segmentio/glue.(*Walker).Walk
	/Users/tejas/dev/src/github.com/segmentio/glue/walker.go:49 +0x195

Handle import name collisions

    "github.com/segmentio/something-service/something"
    "github.com/segmentio/something"

Glue produced code that imported two packages with the same name, resulting in a compile error. We should probably detect collisions and rename the imports by appending an incrementing number or something. LMK if someone has a better idea!

Support context

Even though the standard library's net/rpc doesn't support contexts, we should because they're getting more popular and others have asked for it. The big question is-- Should we have a Context suffixed method for every RPC endpoint, or should we make a breaking change and have context.Context as the first argument across the board?

I don't feel strongly. It's easy to add context.TODO() if you don't care about contexts in your caller, but I also don't want to trouble people too much by making a breaking change... though it may be a good opportunity for me (and others) to learn gofix.

(Added assignees for feedback)

Vendor

package github.com/segmentio/db-service/client
imports github.com/segmentio/db-service/vendor/gopkg.in/mgo.v2/bson: must be imported as gopkg.in/mgo.v2/bson

Print when successful, not only for errors

When you run mockery, it'll print "Generated mock: S3API" so you know it did something. Glue should do the same for each file it writes. (so you don't need to use -v to make sure it worked)

Generate a client interface

We may want this. Some people have asked for it, but after mentioning Go is opportunistically typed and you can make your own interface, some have said they can do without it. So... not sure whether or not we want to add this. It's a simple change to the template file if we do! I'd merge any PR that added it.

I added people who asked about it as assignees for feedback on the idea

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.