commit a7ddf5c8461e749313b77c4b40e042bd429b6de7 Author: cubixle Date: Mon Jun 14 20:48:06 2021 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..335d4b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +tuugen diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3e4579f --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +build: + go build ./cmd/tuugen \ No newline at end of file diff --git a/cmd.go b/cmd.go new file mode 100644 index 0000000..5c61832 --- /dev/null +++ b/cmd.go @@ -0,0 +1,16 @@ +package tuugen + +import "os/exec" + +func runCommand(cmd string, args []string) error { + c := exec.Command(cmd, args...) + err := c.Start() + if err != nil { + return nil + } + err = c.Wait() + if err != nil { + return err + } + return nil +} diff --git a/example_project/internal/pb/service/service.pb.go b/example_project/internal/pb/service/service.pb.go new file mode 100644 index 0000000..c5b8f6c --- /dev/null +++ b/example_project/internal/pb/service/service.pb.go @@ -0,0 +1,166 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.6.1 +// source: service.proto + +package service + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type User struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + TeamId string `protobuf:"bytes,2,opt,name=team_id,json=teamId,proto3" json:"team_id,omitempty"` + Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"` +} + +func (x *User) Reset() { + *x = User{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *User) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*User) ProtoMessage() {} + +func (x *User) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use User.ProtoReflect.Descriptor instead. +func (*User) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{0} +} + +func (x *User) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *User) GetTeamId() string { + if x != nil { + return x.TeamId + } + return "" +} + +func (x *User) GetEmail() string { + if x != nil { + return x.Email + } + return "" +} + +var File_service_proto protoreflect.FileDescriptor + +var file_service_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x49, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, + 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x17, 0x0a, 0x07, 0x74, 0x65, 0x61, 0x6d, 0x5f, 0x69, 0x64, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x74, 0x65, 0x61, 0x6d, 0x49, 0x64, 0x12, 0x14, 0x0a, + 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x6d, + 0x61, 0x69, 0x6c, 0x32, 0x37, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x2c, + 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x0d, 0x2e, 0x73, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x1a, 0x0d, 0x2e, 0x73, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x22, 0x00, 0x42, 0x15, 0x5a, 0x13, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x62, 0x2f, 0x73, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_service_proto_rawDescOnce sync.Once + file_service_proto_rawDescData = file_service_proto_rawDesc +) + +func file_service_proto_rawDescGZIP() []byte { + file_service_proto_rawDescOnce.Do(func() { + file_service_proto_rawDescData = protoimpl.X.CompressGZIP(file_service_proto_rawDescData) + }) + return file_service_proto_rawDescData +} + +var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_service_proto_goTypes = []interface{}{ + (*User)(nil), // 0: service.User +} +var file_service_proto_depIdxs = []int32{ + 0, // 0: service.Service.CreateUser:input_type -> service.User + 0, // 1: service.Service.CreateUser:output_type -> service.User + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_service_proto_init() } +func file_service_proto_init() { + if File_service_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_service_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*User); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_service_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_service_proto_goTypes, + DependencyIndexes: file_service_proto_depIdxs, + MessageInfos: file_service_proto_msgTypes, + }.Build() + File_service_proto = out.File + file_service_proto_rawDesc = nil + file_service_proto_goTypes = nil + file_service_proto_depIdxs = nil +} diff --git a/example_project/internal/pb/service/service_grpc.pb.go b/example_project/internal/pb/service/service_grpc.pb.go new file mode 100644 index 0000000..686b73e --- /dev/null +++ b/example_project/internal/pb/service/service_grpc.pb.go @@ -0,0 +1,101 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package service + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// ServiceClient is the client API for Service service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type ServiceClient interface { + CreateUser(ctx context.Context, in *User, opts ...grpc.CallOption) (*User, error) +} + +type serviceClient struct { + cc grpc.ClientConnInterface +} + +func NewServiceClient(cc grpc.ClientConnInterface) ServiceClient { + return &serviceClient{cc} +} + +func (c *serviceClient) CreateUser(ctx context.Context, in *User, opts ...grpc.CallOption) (*User, error) { + out := new(User) + err := c.cc.Invoke(ctx, "/service.Service/CreateUser", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ServiceServer is the server API for Service service. +// All implementations must embed UnimplementedServiceServer +// for forward compatibility +type ServiceServer interface { + CreateUser(context.Context, *User) (*User, error) + mustEmbedUnimplementedServiceServer() +} + +// UnimplementedServiceServer must be embedded to have forward compatible implementations. +type UnimplementedServiceServer struct { +} + +func (UnimplementedServiceServer) CreateUser(context.Context, *User) (*User, error) { + return nil, status.Errorf(codes.Unimplemented, "method CreateUser not implemented") +} +func (UnimplementedServiceServer) mustEmbedUnimplementedServiceServer() {} + +// UnsafeServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ServiceServer will +// result in compilation errors. +type UnsafeServiceServer interface { + mustEmbedUnimplementedServiceServer() +} + +func RegisterServiceServer(s grpc.ServiceRegistrar, srv ServiceServer) { + s.RegisterService(&Service_ServiceDesc, srv) +} + +func _Service_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(User) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ServiceServer).CreateUser(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/service.Service/CreateUser", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ServiceServer).CreateUser(ctx, req.(*User)) + } + return interceptor(ctx, in, info, handler) +} + +// Service_ServiceDesc is the grpc.ServiceDesc for Service service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Service_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "service.Service", + HandlerType: (*ServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "CreateUser", + Handler: _Service_CreateUser_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "service.proto", +} diff --git a/example_project/service.proto b/example_project/service.proto new file mode 100644 index 0000000..b25bfc5 --- /dev/null +++ b/example_project/service.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package service; + +option go_package = 'internal/pb/service'; + +service Service { + rpc CreateUser(User) returns (User) {}; +} + +message User { + string name = 1; + string team_id = 2; + string email = 3; +} diff --git a/example_project/tuugen.yml b/example_project/tuugen.yml new file mode 100644 index 0000000..2ecb858 --- /dev/null +++ b/example_project/tuugen.yml @@ -0,0 +1,24 @@ +project: tuugen_test_project +proto: service.proto +db_models: + - name: User + - properties: + - name: id + type: varchar + autoinc: true + - name: team_id + type: varchar + - name: name + type: varchar + - name: email + type: varchar + - name: created_at + type: timestamp + - name: updated_at + type: timestamp + - name: Team + - properties: + - name: id + type: varchar + - name: name + type: varchar diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4cb0dbb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/cubixle/tuugen + +go 1.16 diff --git a/protos.go b/protos.go new file mode 100644 index 0000000..de29fb4 --- /dev/null +++ b/protos.go @@ -0,0 +1,7 @@ +package tuugen + +func GenerateProtos() error { + cmd := "protoc" + args := []string{"-I", ".:${GOPATH}/src", "--go_out=.", "--go-grpc_out=.", "service.proto"} + return runCommand(cmd, args) +} diff --git a/service.go b/service.go new file mode 100644 index 0000000..43a13d4 --- /dev/null +++ b/service.go @@ -0,0 +1,34 @@ +package tuugen + +import ( + "go/ast" + "go/parser" + "go/token" +) + +type serviceEndpoint struct { + name string + args []string +} + +func GenerateService(filepath string) error { + + // load service template + // t, err := template.ParseFiles("templates/service.go.tmpl") + // if err != nil { + // return err + // } + + // read interface from project built protos. + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, filepath, nil, 0) + if err != nil { + return err + } + // generate the service template and store it to a file. + err = ast.Print(fset, f) + if err != nil { + return err + } + return nil +} diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..1e192a3 --- /dev/null +++ b/templates.go @@ -0,0 +1,4 @@ +package tuugen + +type Templates interface { +} diff --git a/templates/data_obj.go.tmpl b/templates/data_obj.go.tmpl new file mode 100644 index 0000000..e69de29 diff --git a/templates/main.go.tmpl b/templates/main.go.tmpl new file mode 100644 index 0000000..1723c37 --- /dev/null +++ b/templates/main.go.tmpl @@ -0,0 +1,5 @@ +package main + +func main() { + +} \ No newline at end of file diff --git a/templates/service.go.tmpl b/templates/service.go.tmpl new file mode 100644 index 0000000..c69584c --- /dev/null +++ b/templates/service.go.tmpl @@ -0,0 +1,5 @@ +type Service struct { + +} + +func {{ name }}() \ No newline at end of file