如何从f#初始化重复字段(Google.Protocol.ProtoBuf) ?



我有一个Google.Protocol.ProtoBuf消息定义为:

message Rectangle {
int32 X = 1;
int32 Y = 2;
int32 Width = 3;
int32 Height =4;
}
message Word {
google.protobuf.Int32Value VocabularyId = 1;
bytes Strokes      = 2;
string Text        = 3;
string Confidence  = 4;
Rectangle BoundingBox =5;
}
message SaveVocabularyRequest {
repeated Word Words =1 ;
}
在f#中,如何初始化重复字段"Words"?(使用:https://github.com/Arshia001/FSharp.GrpcCodeGenerator)

这失败:

let h = words |> Seq.map InkWordToWord |> Seq.toList

{ Protocol.ProtoBuf.SaveVocabularyRequest.empty() with Words = { Words = {h}}   }

错误消息:类型为'Protocol.ProtoBuf.SaveVocabularyRequest'的字段'_UnknownFields'没有赋值

提前谢谢你。

let InkWordToWord (w:Models.InkWord) : Protocol.ProtoBuf.InkWord = 
let rectangle (dr: Drawing.Rectangle) : Protocol.ProtoBuf.Rectangle =
{Protocol.ProtoBuf.Rectangle.empty() with X = ValueSome dr.X; Y = ValueSome dr.Y; Width = ValueSome dr.Width; Height = ValueSome dr.Height }

let gg (i:int) : Int32Value =
let g = Int32Value.empty()
g.Value <- ValueSome i
g
let toBytes (ss:System.Windows.Ink.StrokeCollection) : byte[] = 
let ms = new MemoryStream()  
ss.Save(ms)
ms.ToArray()


{ Protocol.ProtoBuf.InkWord.empty() with VocabularyId = match w.VocabularyId with
| None -> ValueNone
| Some s -> ValueSome (gg s) ;
Strokes = ValueSome (Google.Protobuf.ByteString.CopyFrom(toBytes w.Strokes));
Text = ValueSome w.Text;
Confidence = ValueSome w.Confidence;
BoundingBox = ValueSome (rectangle w.BoundingBox);
}
let SaveVocabularyRequestToGrpc (words:seq<Models.InkWord>) : SaveVocabularyRequest = 
let v =
let h = words |> Seq.map InkWordToWord |> Seq.toList
let r = Protocol.ProtoBuf.Vocabulary.empty()
// InkWords is a Collection (i.e RepeatedField) in Protobuf.
r.InkWords.Add(h)
r

{Protocol.ProtoBuf.SaveVocabularyRequest.empty() with Vocabulary = ValueSome v }

最新更新