Beispiel #1
0
// generateServerSignature returns the server-side signature for a method.
func (g *grpc) generateServerSignature(servName string, method *pb.MethodDescriptorProto) string {
	origMethName := method.GetName()
	methName := generator.CamelCase(origMethName)
	if reservedClientName[methName] {
		methName += "_"
	}

	var reqArgs []string
	ret := "error"
	if !method.GetServerStreaming() && !method.GetClientStreaming() {
		reqArgs = append(reqArgs, contextPkg+".Context")
		ret = "(*" + g.typeName(method.GetOutputType()) + ", error)"
	}
	if !method.GetClientStreaming() {
		reqArgs = append(reqArgs, "*"+g.typeName(method.GetInputType()))
	}
	if method.GetServerStreaming() || method.GetClientStreaming() {
		reqArgs = append(reqArgs, servName+"_"+generator.CamelCase(origMethName)+"Server")
	}

	return methName + "(" + strings.Join(reqArgs, ", ") + ") " + ret
}
Beispiel #2
0
func (g *grpc) generateServerMethod(servName string, method *pb.MethodDescriptorProto) string {
	methName := generator.CamelCase(method.GetName())
	hname := fmt.Sprintf("_%s_%s_Handler", servName, methName)
	inType := g.typeName(method.GetInputType())
	outType := g.typeName(method.GetOutputType())

	g.P("// methName:         ", methName)
	g.P("// hname:            ", hname)
	g.P("// servName:         ", servName)
	g.P("// method.GetName(): ", method.GetName())
	g.P("// inType:           ", inType)
	g.P("// outType:          ", outType)
	g.P()
	serverType := servName + "Server"

	g.P("func (impl* _", serverType, " )_", methName, "(c web.C, w http.ResponseWriter, r *http.Request) {")

	g.P("	in := ", inType, "{}")
	g.P("	content, err := ioutil.ReadAll(r.Body)")
	g.P("	defer r.Body.Close()")
	g.P("	if err != nil {")
	g.P("		w.WriteHeader(400)")
	g.P("		w.Write([]byte(err.Error()))")
	g.P("		fmt.Println(err.Error())")
	g.P("		return")
	g.P("	}")
	g.P("	err = json.Unmarshal(content, in)")
	g.P("	if err != nil {")
	g.P("		w.WriteHeader(400)")
	g.P("		w.Write([]byte(err.Error()))")
	g.P("		fmt.Println(err.Error())")
	g.P("		return")
	g.P("	}")
	//g.P("	return err")
	//g.P("	if request2Json(r, &in, w) != nil {")
	//g.P("		return")
	//g.P("	}")
	//g.P("	json.NewEncoder(w).Encode(handler.logic.setContext(&c).ConnectionTest(in))")
	g.P("	res,err := impl.handler.", methName, "(context.TODO(),&in)")
	g.P("	if err != nil {")
	g.P("		w.WriteHeader(400)")
	g.P("		w.Write([]byte(err.Error()))")
	g.P("		fmt.Println(err.Error())")
	g.P("		return")
	g.P("	}")
	g.P("	json.NewEncoder(w).Encode(res)")
	g.P("}")
	g.P()

	/*if !method.GetServerStreaming() && !method.GetClientStreaming() {
		g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, codec ", grpcPkg, ".Codec, buf []byte) (interface{}, error) {")
		g.P("in := new(", inType, ")")
		g.P("if err := codec.Unmarshal(buf, in); err != nil { return nil, err }")
		g.P("out, err := srv.(", servName, "Server).", methName, "(ctx, in)")
		g.P("if err != nil { return nil, err }")
		g.P("return out, nil")
		g.P("}")
		g.P()
		return hname
	}
	streamType := unexport(servName) + methName + "Server"
	g.P("func ", hname, "(srv interface{}, stream ", grpcPkg, ".ServerStream) error {")
	if !method.GetClientStreaming() {
		g.P("m := new(", inType, ")")
		g.P("if err := stream.RecvMsg(m); err != nil { return err }")
		g.P("return srv.(", servName, "Server).", methName, "(m, &", streamType, "{stream})")
	} else {
		g.P("return srv.(", servName, "Server).", methName, "(&", streamType, "{stream})")
	}
	g.P("}")
	g.P()

	genSend := method.GetServerStreaming()
	genSendAndClose := !method.GetServerStreaming()
	genRecv := method.GetClientStreaming()

	// Stream auxiliary types and methods.
	g.P("type ", servName, "_", methName, "Server interface {")
	if genSend {
		g.P("Send(*", outType, ") error")
	}
	if genSendAndClose {
		g.P("SendAndClose(*", outType, ") error")
	}
	if genRecv {
		g.P("Recv() (*", inType, ", error)")
	}
	g.P(grpcPkg, ".ServerStream")
	g.P("}")
	g.P()

	g.P("type ", streamType, " struct {")
	g.P(grpcPkg, ".ServerStream")
	g.P("}")
	g.P()

	if genSend {
		g.P("func (x *", streamType, ") Send(m *", outType, ") error {")
		g.P("return x.ServerStream.SendMsg(m)")
		g.P("}")
		g.P()
	}
	if genSendAndClose {
		g.P("func (x *", streamType, ") SendAndClose(m *", outType, ") error {")
		g.P("return x.ServerStream.SendMsg(m)")
		g.P("}")
		g.P()
	}
	if genRecv {
		g.P("func (x *", streamType, ") Recv() (*", inType, ", error) {")
		g.P("m := new(", inType, ")")
		g.P("if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err }")
		g.P("return m, nil")
		g.P("}")
		g.P()
	}
	*/
	g.P()
	return hname
}
Beispiel #3
0
// generateService generates all the code for the named service.
func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
	//path := fmt.Sprintf("6,%d", index) // 6 means service.

	origServName := service.GetName()
	fullServName := file.GetPackage() + "." + origServName
	servName := generator.CamelCase(origServName)
	/*
		g.P()
		g.P("// Client API for ", servName, " service")
		g.P()

		// Client interface.
		g.P("type ", servName, "Client interface {")
		for i, method := range service.Method {
			g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
			g.P(g.generateClientSignature(servName, method))
		}
		g.P("}")
		g.P()

		// Client structure.
		g.P("type ", unexport(servName), "Client struct {")
		g.P("cc *", grpcPkg, ".ClientConn")
		g.P("}")
		g.P()

		// NewClient factory.
		g.P("func New", servName, "Client (cc *", grpcPkg, ".ClientConn) ", servName, "Client {")
		g.P("return &", unexport(servName), "Client{cc}")
		g.P("}")
		g.P()

		var methodIndex, streamIndex int
	*/
	serviceDescVar := "_" + servName + "_serviceDesc"
	/*
		// Client method implementations.
		for _, method := range service.Method {
			var descExpr string
			if !method.GetServerStreaming() && !method.GetClientStreaming() {
				// Unary RPC method
				descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex)
				methodIndex++
			} else {
				// Streaming RPC method
				descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex)
				streamIndex++
			}
			g.generateClientMethod(servName, fullServName, serviceDescVar, method, descExpr)
		}
	*/
	g.P("// Server API for ", servName, " service")
	g.P()

	g.P("// fullServName:   ", fullServName)
	g.P("// serviceDescVar: ", serviceDescVar)
	g.P("// servName:       ", servName)
	g.P()

	// Server interface.
	serverType := servName + "Server"
	//g.P("type ", serverType, " interface {")
	//for i, method := range service.Method {
	//	g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
	//	g.P(g.generateServerSignature(servName, method))
	//}
	//g.P("}")
	g.P()

	g.P("func NewHandler(h ", serverType, ", prefix string) *web.Mux {")
	g.P("	t := _", serverType, "{}")
	g.P("	t.handler = h")
	g.P("	router := web.New()")
	for _, method := range service.Method {
		path := strings.ToLower(servName) + "/" + method.GetName()
		methName := generator.CamelCase(method.GetName())
		// there should be a better way to get the options
		m := method.GetOptions().String()
		if m != "" {
			parts := strings.Split(m, "\"")
			if len(parts) == 3 {
				if parts[0] == "10000:" {
					path = parts[1]
				}
			}
		}
		g.P("router.Handle(prefix+\"", strings.ToLower(path), "\", t._", methName, ")")
	}
	g.P("	return router")
	g.P("}")
	g.P()

	g.P("type _", serverType, " struct {")
	g.P("	handler ", serverType)
	g.P("}")
	g.P()

	// Server registration.
	//g.P("func Register", servName, "Server(s *", grpcPkg, ".Server, srv ", serverType, ") {")
	//g.P("s.RegisterService(&", serviceDescVar, `, srv)`)
	//g.P("}")
	//g.P()

	// Server handler implementations.
	//var handlerNames []string
	for _, method := range service.Method {
		g.generateServerMethod(servName, method)
		//hname := g.generateServerMethod(servName, method)
		//handlerNames = append(handlerNames, hname)
	}

	// Service descriptor.
	/*
		g.P("var ", serviceDescVar, " = ", grpcPkg, ".ServiceDesc {")
		g.P("ServiceName: ", strconv.Quote(fullServName), ",")
		g.P("HandlerType: (*", serverType, ")(nil),")
		g.P("Methods: []", grpcPkg, ".MethodDesc{")
		for i, method := range service.Method {
			if method.GetServerStreaming() || method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("MethodName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			g.P("},")
		}
		g.P("},")
		g.P("Streams: []", grpcPkg, ".StreamDesc{")
		for i, method := range service.Method {
			if !method.GetServerStreaming() && !method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("StreamName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			if method.GetServerStreaming() {
				g.P("ServerStreams: true,")
			}
			if method.GetClientStreaming() {
				g.P("ClientStreams: true,")
			}
			g.P("},")
		}
		g.P("},")
		g.P("}")
		g.P()
	*/
}