Go converting ptypes/struct Value to BSON

跟風遠走 提交于 2020-03-23 09:50:08

问题


Requirements

Two services:

  • Server - for writing blog posts to MongoDB
  • Client - sends request to the first service

The blog post has title of type string, and content which is a dynamic type - can be any JSON value.

Protobuf

syntax = "proto3";

package blog;

option go_package = "blogpb";

import "google/protobuf/struct.proto";

message Blog {
  string id = 1;
  string title = 2;
  google.protobuf.Value content = 3;
}

message CreateBlogRequest {
  Blog blog = 1;
}

message CreateBlogResponse {
  Blog blog = 1;
}

service BlogService {
  rpc CreateBlog (CreateBlogRequest) returns (CreateBlogResponse);
}

Let's start with protobuf message, which meats requirements - string for title and any JSON value for content.

Client

package main

import (...)

func main() {
    cc, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
    defer cc.Close()
    c := blogpb.NewBlogServiceClient(cc)

    var blog blogpb.Blog

    json.Unmarshal([]byte(`{"title": "First example", "content": "string"}`), &blog)
    c.CreateBlog(context.Background(), &blogpb.CreateBlogRequest{Blog: &blog})

    json.Unmarshal([]byte(`{"title": "Second example", "content": {"foo": "bar"}}`), &blog)
    c.CreateBlog(context.Background(), &blogpb.CreateBlogRequest{Blog: &blog})
}

The client sends two requests to the server - one with content having string type, and other with the object. No errors here.

Server

package main

import (...)

var collection *mongo.Collection

type server struct {
}

type blogItem struct {
    ID      primitive.ObjectID `bson:"_id,omitempty"`
    Title   string             `bson:"title"`
    Content *_struct.Value     `bson:"content"`
}

func (*server) CreateBlog(ctx context.Context, req *blogpb.CreateBlogRequest) (*blogpb.CreateBlogResponse, error) {
    blog := req.GetBlog()

    data := blogItem{
        Title:   blog.GetTitle(),
        Content: blog.GetContent(),
    }

    // TODO: convert "data" or "data.Content" to something that could be BSON encoded..

    res, err := collection.InsertOne(context.Background(), data)
    if err != nil {
        log.Fatal(err)
    }
    oid, _ := res.InsertedID.(primitive.ObjectID)

    return &blogpb.CreateBlogResponse{
        Blog: &blogpb.Blog{
            Id:      oid.Hex(),
            Title:   blog.GetTitle(),
            Content: blog.GetContent(),
        },
    }, nil

}

func main() {
    client, _ := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
    client.Connect(context.TODO())
    collection = client.Database("mydb").Collection("blog")
    lis, _ := net.Listen("tcp", "0.0.0.0:50051")
    s := grpc.NewServer([]grpc.ServerOption{}...)
    blogpb.RegisterBlogServiceServer(s, &server{})
    reflection.Register(s)
    go func() { s.Serve(lis) }()
    ch := make(chan os.Signal, 1)
    signal.Notify(ch, os.Interrupt)
    <-ch
    client.Disconnect(context.TODO())
    lis.Close()
    s.Stop()
}

And here I get:

cannot transform type main.blogItem to a BSON Document: no encoder found for structpb.isValue_Kind

What do I expect? To see the exact value of content in MongoDB, something like this:

{ "_id" : ObjectId("5e5f6f75d2679d058eb9ef79"), "title" : "Second example", "content": "string" }
{ "_id" : ObjectId("5e5f6f75d2679d058eb9ef78"), "title" : "First example", "content": { "foo": "bar" } }

I guess I need to transform data.Content in the line where I added TODO:...

I can create github repo with this example if that could help.


回答1:


So as suggested by @nguyenhoai890 in the comment I managed to fix it using jsonpb lib - first MarshalToString to covert from structpb to string(json) and then json.Unmarshal to convert from string(json) to interface{} which is supported by BSON. Also I had to fix a Client to correctly unmarshal from string to protobuf.

Client

func main() {
    cc, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
    defer cc.Close()
    c := blogpb.NewBlogServiceClient(cc)

    var blog blogpb.Blog
    jsonpb.UnmarshalString(`{"title": "Second example", "content": {"foo": "bar"}}`, &blog)
    c.CreateBlog(context.Background(), &blogpb.CreateBlogRequest{Blog: &blog})

    jsonpb.UnmarshalString(`{"title": "Second example", "content": "stirngas"}`, &blog)
    c.CreateBlog(context.Background(), &blogpb.CreateBlogRequest{Blog: &blog})
}

Server

type blogItem struct {
    ID      primitive.ObjectID `bson:"_id,omitempty"`
    Title   string             `bson:"title"`
    Content interface{}        `bson:"content"`
}

func (*server) CreateBlog(ctx context.Context, req *blogpb.CreateBlogRequest) (*blogpb.CreateBlogResponse, error) {
    blog := req.GetBlog()

    contentString, err := new(jsonpb.Marshaler).MarshalToString(blog.GetContent())
    if err != nil {
        log.Fatal(err)
    }

    var contentInterface interface{}
    json.Unmarshal([]byte(contentString), &contentInterface)

    data := blogItem{
        Title:   blog.GetTitle(),
        Content: contentInterface,
    }

    res, err := collection.InsertOne(context.Background(), data)
    if err != nil {
        log.Fatal(err)
    }
    oid, _ := res.InsertedID.(primitive.ObjectID)

    return &blogpb.CreateBlogResponse{
        Blog: &blogpb.Blog{
            Id:      oid.Hex(),
            Title:   blog.GetTitle(),
            Content: blog.GetContent(),
        },
    }, nil

}


来源:https://stackoverflow.com/questions/60523142/go-converting-ptypes-struct-value-to-bson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!