Git Product home page Git Product logo

gogo-struct-codec's Introduction

gogo-struct-codec

codec of the Well Known google.protobuf.Struct type which generated by gogo protobuf

Drop-in replacement for gogo's types.Struct

The ptypes.Struct is provided as a drop-in replacement for the gogo's types.Struct which doesn't be implemented with common Marshal/Unmarshal interfaces.

Currently ptypes.Struct is implemented by embedding gogo's types.Struct and with the following interfaces:

  • json.Marshaler and json.Unmarshaler (by gogo's jsonpb)
  • sql.Scanner and sql/driver.Valuer (by gogo's jsonpb)
  • bson.Marshaler and bson.Unmarshaler (by structbson)

Drop-in example

Please take ./example a look.

syntax = "proto3";
package example;

import "google/protobuf/struct.proto";

message MyMessage {
    google.protobuf.Struct payload = 1;
}

And please take ./example/gen.sh a look as well. There is how to tell the gogo output plugin to replace the Struct with our one.

./gen.sh

BSON Codec

Convert the gogo's types.Struct from and to bson

MongoDB example

package main

import (
    "context"
    "github.com/gogo/protobuf/types"
    "github.com/rueian/gogo-struct-codec/structbson"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
)

func main() {
    
    // MUST first replace bson's DefaultRegistry with the one in structbson
    bson.DefaultRegistry = structbson.DefaultRegistry

    s := types.Struct{Fields:map[string]*types.Value{
        "_id": {Kind: &types.Value_StringValue{StringValue: "str"}},
    }}
    
    // connect mongoclient

    res, err := mongoclient.Database("db").Collection("collection").InsertOne(ctx, s)
    
    // handle err and response 
}

BSON manipulation

more details in test cases

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.