簡體   English   中英

將 ptypes/struct Value 轉換為 BSON

[英]Go converting ptypes/struct Value to BSON

要求

兩項服務:

  • 服務器- 用於將博客文章寫入 MongoDB
  • 客戶端- 向第一個服務發送請求

博客文章的title類型為stringcontent為動態類型 - 可以是任何 JSON 值。

原緩沖區

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);
}

讓我們從 protobuf 消息開始,它滿足要求 - title stringcontent任何 JSON 值。

客戶

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})
}

客戶端向服務器發送兩個請求 - 一個contentstring類型,另一個請求為object 這里沒有錯誤。

服務器

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()
}

在這里我得到:

無法將 main.blogItem 類型轉換為 BSON 文檔:未找到 structpb.isValue_Kind 的編碼器

我期待什么? 要查看 MongoDB 中內容的確切值,如下所示:

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

我想我需要在添加TODO:的行中轉換data.Content TODO: ...

如果有幫助,我可以用這個例子創建 github repo。

因此,正如@nguyenhoai890 在評論中所建議的,我設法使用jsonpb lib 修復它 - 首先MarshalToStringstructpbstring(json) ,然后json.Unmarshalstring(json)轉換為interface{} ,這是 BSON 支持的. 此外,我必須修復客戶端以正確地從字符串解組到 protobuf。

客戶

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})
}

服務器

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM