Skip to content

Commit

Permalink
feat(jzero): ivm init support stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Jul 7, 2024
1 parent 9bafc80 commit cf9044e
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 266 deletions.
24 changes: 24 additions & 0 deletions .template/ivm/init/logic-body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
logic := {{ .OldService }}logic.New{{ .LogicTypeName | FirstUpper }}(l.ctx, l.svcCtx)
marshal, err := proto.Marshal(in)
if err != nil {
return nil, err
}
var oldIn {{ .OldService }}pb.{{ .RequestTypeName }}
err = proto.Unmarshal(marshal, &oldIn)
if err != nil {
return nil, err
}
result, err := logic.{{ .MethodName }}(&oldIn)
if err != nil {
return nil, err
}
marshal, err = proto.Marshal(result)
if err != nil {
return nil, err
}
var newResp {{ .Service }}pb.{{ .ResponseTypeName }}
err = proto.Unmarshal(marshal, &newResp)
if err != nil {
return nil, err
}
return &newResp, nil
49 changes: 49 additions & 0 deletions .template/ivm/init/logic-client-server-stream-adaptor.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
type {{ .MethodName | FirstUpper }}ServerAdapter struct {
{{ .Service }}pb.{{ .Service | FirstUpper }}_{{ .MethodName | FirstUpper }}Server
}

func (s {{ .MethodName | FirstUpper }}ServerAdapter) Send(response *{{ .OldService }}pb.{{ .ResponseTypeName }}) error {
marshal, err := proto.Marshal(response)
if err != nil {
return err
}
var newResp {{ .Service }}pb.{{ .ResponseTypeName }}
err = proto.Unmarshal(marshal, &newResp)
if err != nil {
return err
}

err = s.SendMsg(&newResp)
if err != nil {
if err == io.EOF {
return nil
}
return err
}

return nil
}

func (s {{ .MethodName | FirstUpper }}ServerAdapter) Recv() (*{{ .OldService }}pb.{{ .RequestTypeName }}, error) {
for {
newIn, err := s.{{ .Service | FirstUpper }}_{{ .MethodName | FirstUpper }}Server.Recv()
if err == io.EOF {
return nil, io.EOF
}
if err != nil {
return nil, err
}

marshal, err := proto.Marshal(newIn)
if err != nil {
return nil, err
}
var oldIn {{ .OldService }}pb.{{ .RequestTypeName }}
err = proto.Unmarshal(marshal, &oldIn)
if err != nil {
return nil, err
}

return &oldIn, nil
}
}
8 changes: 8 additions & 0 deletions .template/ivm/init/logic-client-server-stream-body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
logic := {{ .OldService }}logic.New{{ .LogicTypeName }}(l.ctx, l.svcCtx)

// Create the adapter
adapter := &{{ .MethodName | FirstUpper }}ServerAdapter{
stream,
}

return logic.{{ .MethodName | FirstUpper }}(adapter)
41 changes: 41 additions & 0 deletions .template/ivm/init/logic-client-stream-adaptor.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type {{ .MethodName | FirstUpper }}ServerAdapter struct {
{{ .Service }}pb.{{ .Service | FirstUpper }}_{{ .MethodName | FirstUpper }}Server
}

func (s *{{ .MethodName | FirstUpper }}ServerAdapter) SendAndClose(response *{{ .OldService }}pb.{{ .ResponseTypeName }}) error {
marshal, err := proto.Marshal(response)
if err != nil {
return err
}

var newResp {{ .Service }}pb.{{ .ResponseTypeName }}
err = proto.Unmarshal(marshal, &newResp)
if err != nil {
return err
}
return s.SendMsg(&newResp)
}

func (s *{{ .MethodName | FirstUpper }}ServerAdapter) Recv() (*{{ .OldService }}pb.{{ .RequestTypeName }}, error) {
for {
newIn, err := s.{{ .Service | FirstUpper }}_{{ .MethodName | FirstUpper }}Server.Recv()
if err == io.EOF {
return nil, io.EOF
}
if err != nil {
return nil, err
}

marshal, err := proto.Marshal(newIn)
if err != nil {
return nil, err
}
var oldIn {{ .OldService }}pb.{{ .RequestTypeName }}
err = proto.Unmarshal(marshal, &oldIn)
if err != nil {
return nil, err
}

return &oldIn, nil
}
}
8 changes: 8 additions & 0 deletions .template/ivm/init/logic-client-stream-body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
logic := {{ .OldService }}logic.New{{ .LogicTypeName }}(l.ctx, l.svcCtx)

// Create the adapter
adapter := &{{ .MethodName | FirstUpper }}ServerAdapter{
stream,
}

return logic.{{ .MethodName | FirstUpper }}(adapter)
25 changes: 25 additions & 0 deletions .template/ivm/init/logic-server-stream-adaptor.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type {{ .MethodName | FirstUpper }}ServerAdapter struct {
{{ .Service }}pb.{{ .Service | FirstUpper }}_{{ .MethodName | FirstUpper }}Server
}

func (s *{{ .MethodName | FirstUpper }}ServerAdapter) Send(response *{{ .OldService }}pb.{{ .ResponseTypeName }}) error {
marshal, err := proto.Marshal(response)
if err != nil {
return err
}
var newResp {{ .Service }}pb.{{ .ResponseTypeName }}
err = proto.Unmarshal(marshal, &newResp)
if err != nil {
return err
}

err = s.SendMsg(&newResp)
if err != nil {
if err == io.EOF {
return nil
}
return err
}

return nil
}
19 changes: 19 additions & 0 deletions .template/ivm/init/logic-server-stream-body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logic := {{ .OldService }}logic.New{{ .LogicTypeName }}(l.ctx, l.svcCtx)

// Create the adapter
adapter := &{{ .MethodName | FirstUpper }}ServerAdapter{
stream,
}

marshal, err := proto.Marshal(in)
if err != nil {
return err
}

var oldIn {{ .OldService }}pb.{{ .RequestTypeName }}
err = proto.Unmarshal(marshal, &oldIn)
if err != nil {
return err
}

return logic.{{ .MethodName | FirstUpper }}(&oldIn, adapter)
6 changes: 4 additions & 2 deletions internal/gen/genapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ type LogicFile struct {
Handler string
Path string

ClientStream bool
ServerStream bool
RequestTypeName string
ResponseTypeName string
ClientStream bool
ServerStream bool
}

func (ja *JzeroApi) Gen() error {
Expand Down
12 changes: 7 additions & 5 deletions internal/gen/genrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ func (jr *JzeroRpc) GetAllLogicFiles(protoSpec rpcparser.Proto) ([]LogicFile, er
fp := filepath.Join(jr.Wd, "internal", "logic", strings.ToLower(service.Name), namingFormat+".go")

f := LogicFile{
Path: fp,
Handler: rpc.Name,
Group: service.Name,
ClientStream: rpc.StreamsRequest,
ServerStream: rpc.StreamsReturns,
Path: fp,
Handler: rpc.Name,
Group: service.Name,
ClientStream: rpc.StreamsRequest,
ServerStream: rpc.StreamsReturns,
ResponseTypeName: rpc.ReturnsType,
RequestTypeName: rpc.RequestType,
}

logicFiles = append(logicFiles, f)
Expand Down
2 changes: 1 addition & 1 deletion internal/ivm/ivminit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Init(command *cobra.Command, args []string) error {

for i, fp := range newVersionProtoFilepath {
oldFp := protoFiles[i]
err = ivmInit.setUpdateProtoLogic(fp, oldFp)
err = ivmInit.updateProtoLogic(fp, oldFp)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cf9044e

Please sign in to comment.