Skip to content
/ morph Public

๐ŸŒŠ Conversion tool between protoc stub and struct

License

Notifications You must be signed in to change notification settings

xoxoist/morph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

33 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Morph

xoxoist codecov Sourcegraph Go Report Card GoDoc 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

Installation

  1. Required go installed on your machine
go version
  1. Get morph
go get -u github.com/xoxoist/morph
  1. Import morph
import "github.com/xoxoist/morph"

Quick Start

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

    // 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