duanraa1984 2019-09-12 13:59
浏览 399
已采纳

如何在Proto3中为HTTP响应创建可空字段?

I want to return an object as an HTTP response where one of its fields is nullable. The problem is proto3 won't let me do it easily. This happens because I parsed a pointer of string to a string, so when the pointer points to null it produces this error runtime error: invalid memory address or nil pointer dereference

I have attempted to solve this by at least these two work-arounds I learned from the Internet.

1. Using oneof

exercise.proto (the message definition)

message ExercisesData {
    string Serial = 1 [json_name="serial"];
    string Title = 2 [json_name="title"];
    oneof OptionalSubmissionSerial {
       string SubmissionSerial = 3 [json_name="submission_serial"];
}

mapper.go (to parse a Go struct to fit the proto message)

exercise := &Exercise.ExercisesData {
           Serial:                   e.Serial,
           Title:                    e.Title,
           OptionalSubmissionSerial: &Exercise.ExercisesData_SubmissionSerial{
                SubmissionSerial: *e.SubmissionInfo.LatestSubmissionSerial,
           },
}

2. Using google/protobuf/wrappers.proto

exercise.proto (the message definition)

import "google/protobuf/wrappers.proto";

message ExercisesData {
    string Serial = 1 [json_name="serial"];
    string Title = 2 [json_name="title"];
    google.protobuf.StringValue SubmissionSerial = 3 [json_name="submission_serial"];
}

mapper.go (to parse a Go struct to fit the proto message)

exercise := &Exercise.ExercisesData {
           Serial:                   e.Serial,
           Title:                    e.Title,
           SubmissionSerial:         &wrappers.StringValue{
                Value: *e.SubmissionInfo.LatestSubmissionSerial,
            },
}

Expected Result

Both ways still produce the same error message, the only difference is the line of code it refers to. That's why I am so helpless. The expected HTTP response would look like this

{
    "status": "success",
    "data": [
        {
            "serial": "EXC-NT2OBHQT",
            "title": "Title of Topic Exercise",
            "submission_serial": null
        }
     ]
}

I really hope anyone can help me to find a way to define a nullable field in proto3 for a Http response and how to parse it from a struct. Thank you!

  • 写回答

2条回答 默认 最新

  • dsjpik057730 2019-09-13 11:25
    关注

    turns out I find another workaround that actually works! It's using google/protobuf/wrappers.proto but I gotta tweak it a lil' bit in the mapper. Here's how it goes:

    exercise.proto (the message definition)

    import "google/protobuf/wrappers.proto";
    
    message ExercisesData {
        string Serial = 1 [json_name="serial"];
        string Title = 2 [json_name="title"];
        google.protobuf.StringValue SubmissionSerial = 3 [json_name="submission_serial"];
    }
    

    mapper.go (to parse a Go struct to fit the proto message)

    import "github.com/golang/protobuf/ptypes/wrappers"
    
            exercise := &pbExercise.GetExercisesData{
                Serial:              e.Serial,
                Title:               e.Title,
            }
    
            if e.SubmissionInfo.LatestSubmissionSerial != nil {
                exercise.SubmissionSerial = &wrappers.StringValue{
                    Value: *e.LatestSubmissionSerial,
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器