xgopb

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2024 License: MIT Imports: 5 Imported by: 0

README

xgo pb - protobuf extension

The xgo pb is a protobuf extension of xgo.

Features

  • Deep copy

Install

$ go get github.com/glassonion1/xgo/xgopb

Import

import "github.com/glassonion1/xgo/xgopb"

Usage

Deep copy
package xgopb_test

import (
    "fmt"
    "time"

    "github.com/golang/protobuf/ptypes/timestamp"
    "github.com/glassonion1/xgo/xgopb"
)

// It is a common, ordinary struct
type FromModel struct {
    ID         string `copier:"Id"`
    Name       string
    CreatedAt  time.Time
    UpdatedAt  *time.Time
}
// It is like a protobuf struct on gRPC
type ToModel struct {
    Id         string
    Name       string
    CreatedAt  *timestamp.Timestamp
    UpdatedAt  *timestamp.Timestamp
}

func Example() {
    now := time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC)
    from := FromModel{
        ID: "xxxx",
        Name: "R2D2",
        CreatedAt: now,
        UpdatedAt: &now,
    }
    to := &ToModel{}
    err := xgo.DeepCopy(from, to)
    if err != nil {
        // handles error
    }
    fmt.Println("ToModel object:", to)
    
    // Output: ToModel object: &{xxxx R2D2 seconds:1748736000 seconds:1748736000}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy(srcModel interface{}, dstModel interface{}) error

DeepCopy

Example (Struct)
package main

import (
	"fmt"
	"time"

	"github.com/glassonion1/xgo/xgopb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func main() {
	// It is a common, ordinary struct
	type FromModel struct {
		ID        string `copier:"Id"`
		Name      string
		CreatedAt time.Time
		UpdatedAt *time.Time
	}

	// It is like a protobuf struct on gRPC
	type ToModel struct {
		Id        string
		Name      string
		CreatedAt *timestamppb.Timestamp
		UpdatedAt *timestamppb.Timestamp
	}

	now := time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC)
	from := FromModel{
		ID:        "xxxx",
		Name:      "R2D2",
		CreatedAt: now,
		UpdatedAt: &now,
	}
	to := &ToModel{}
	err := xgopb.DeepCopy(from, to)
	if err != nil {
		// handles error
	}
	fmt.Println("ToModel object:", to)

}
Output:

ToModel object: &{xxxx R2D2 seconds:1748736000 seconds:1748736000}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL