Morph is simple tools that helps you work with protoc stub and struct, where you can convert protoc stub to struct, or otherwise, save your time by copying all attribute data, except (Objects, Slices, Array) to target struct or protoc.
- Required go installed on your machine
go version
- Get morph
go get -u github.com/xoxoist/morph
- Import morph
import "github.com/xoxoist/morph"
package main
import (
"fmt"
"github.com/xoxoist/morph"
"github.com/xoxoist/morph/example/model"
pb "github.com/xoxoist/morph/example/protobuf"
)
func sampleStructToProtoc() *pb.Todo {
var todoProtocBlank = &pb.Todo{}
todoStruct := model.Todo{
ID: "5b9e1416-1f06-4a61-a30a-0dcff164639b",
Name: "Lloyd",
Completed: true,
NumberCode: 129520,
NumberProduct: 25983578228,
Codes: []model.Code{{1}, {4}, {9}},
}
for _, c := range todoStruct.Codes {
todoCodeProtocBlank := &pb.Code{}
morph.Struct(&c).Protoc(todoCodeProtocBlank)
todoProtocBlank.Codes = append(todoProtocBlank.Codes, todoCodeProtocBlank)
}
return todoProtocBlank
}
func sampleProtocToStruct(todoProtoc *pb.Todo) model.Todo {
var todo model.Todo
morph.Protoc(todoProtoc).Struct(&todo)
for _, i := range todoProtoc.Codes {
var code model.Code
morph.Protoc(i).Struct(&code)
todo.Codes = append(todo.Codes, code)
}
return todo
}
func main() {
todoProtoc := sampleStructToProtoc()
fmt.Println(fmt.Sprintf("protoc : %+v", todoProtoc))
todoStruct := sampleProtocToStruct(todoProtoc)
fmt.Println(fmt.Sprintf("struct : %+v", todoStruct))
}
-
morph.Struct(v interface{}) morph.ProtocTransformed
// blank protoc var todoProtocBlank = &pb.Todo{} todoStruct := model.Todo{ ID: "5b9e1416-1f06-4a61-a30a-0dcff164639b", Name: "Lloyd", Completed: true, NumberCode: 129520, NumberProduct: 25983578228, } // binds all struct attributes to protoc attributes morph.Struct(&todoStruct).Protoc(todoProtocBlank)
-
morph.Protoc(v interface{}) morph.StructTransformed
// blank struct var todo model.Todo todoProtoc := &pb.Todo{ Id: "5b9e1416-1f06-4a61-a30a-0dcff164639b", Name: "Lloyd", Completed: true, NumberCode: 129520, NumberProduct: 25983578228, } // binds all protoc attributes to struct attributes morph.Protoc(todoProtoc).Struct(&todo)
-
end