https://github.com/xoxoist/morph
🌊 Conversion tool between protoc stub and struct
https://github.com/xoxoist/morph
go-protobuf protobuf3 protobuffer protoc protoc-plugin protocol protocol-buffers
Last synced: 3 months ago
JSON representation
🌊 Conversion tool between protoc stub and struct
- Host: GitHub
- URL: https://github.com/xoxoist/morph
- Owner: xoxoist
- License: mit
- Created: 2022-11-15T00:41:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-06T09:38:06.000Z (almost 3 years ago)
- Last Synced: 2025-07-03T22:14:17.172Z (10 months ago)
- Topics: go-protobuf, protobuf3, protobuffer, protoc, protoc-plugin, protocol, protocol-buffers
- Language: Go
- Homepage:
- Size: 2.1 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Morph

[](https://github.com/xoxoist/morph)
[](https://codecov.io/gh/xoxoist/morph)
[](https://sourcegraph.com/github.com/xoxoist/morph?badge)
[](https://goreportcard.com/report/github.com/xoxoist/morph)
[](https://pkg.go.dev/github.com/xoxoist/morph?tab=doc)
[](https://raw.githubusercontent.com/xoxoist/morph/main/LICENSE)
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.
## Contents
- [Morph](#morph)
- [Contents](#contents)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [API Examples](#api-examples)
- [Conversion](#conversion)
### Installation
1. Required go installed on your machine
```sh
go version
```
2. Get morph
```sh
go get -u github.com/xoxoist/morph
```
3. Import morph
```go
import "github.com/xoxoist/morph"
```
### Quick Start
```go
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))
}
```
### API Examples
### Conversion
- `morph.Struct(v interface{}) morph.ProtocTransformed`
```go
// 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`
```go
// 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`