@@ -2,7 +2,9 @@ use std::collections::BTreeMap;
22use std:: fmt;
33
44use protobuf:: MessageDyn ;
5- use protobuf:: reflect:: { ReflectFieldRef , ReflectValueRef , RuntimeType } ;
5+ use protobuf:: reflect:: {
6+ FieldDescriptor , ReflectFieldRef , ReflectValueRef , RuntimeFieldType , RuntimeType ,
7+ } ;
68use serde:: Serialize ;
79use serde_json:: { Map , Value , json} ;
810
@@ -19,17 +21,10 @@ pub struct Mapping {
1921#[ serde( untagged) ]
2022pub enum Property {
2123 /// A simple, scalar property.
22- Leaf {
23- #[ serde( rename = "type" ) ]
24- typ : String ,
25- #[ serde( default , flatten) ]
26- parameters : BTreeMap < String , Value > ,
27- } ,
24+ Leaf ( BTreeMap < String , Value > ) ,
2825 /// A sub-document mapping, i.e., an `object` or `nested` field.
2926 Mapping {
30- #[ serde( rename = "type" ) ]
31- typ : String ,
32- #[ serde( default , flatten) ]
27+ #[ serde( flatten) ]
3328 parameters : BTreeMap < String , Value > ,
3429 #[ serde( flatten) ]
3530 properties : Mapping ,
@@ -49,14 +44,13 @@ pub enum InferredType {
4944 Object ,
5045}
5146
52- impl Property {
53- pub fn from_options ( options : & FieldMapping , typ : String ) -> Self {
54- let mut params = other_to_json ( options as & dyn MessageDyn ) ;
55- params. remove ( "type" ) ;
56- Self :: Leaf {
57- typ,
58- parameters : params. into_iter ( ) . collect ( ) ,
59- }
47+ impl From < & FieldMapping > for Property {
48+ fn from ( options : & FieldMapping ) -> Self {
49+ Self :: Leaf (
50+ other_to_json ( options as & dyn MessageDyn )
51+ . into_iter ( )
52+ . collect ( ) ,
53+ )
6054 }
6155}
6256
@@ -94,6 +88,15 @@ impl From<RuntimeType> for InferredType {
9488 }
9589}
9690
91+ impl From < & FieldDescriptor > for InferredType {
92+ fn from ( field : & FieldDescriptor ) -> Self {
93+ match field. runtime_field_type ( ) {
94+ RuntimeFieldType :: Singular ( t) | RuntimeFieldType :: Repeated ( t) => Self :: from ( t) ,
95+ RuntimeFieldType :: Map ( _, _) => Self :: Object ,
96+ }
97+ }
98+ }
99+
97100fn to_json ( message : & dyn MessageDyn ) -> Value {
98101 match message. descriptor_dyn ( ) . full_name ( ) {
99102 "google.protobuf.Struct" => struct_to_json ( message) ,
0 commit comments