Skip to content

Commit 1e2cb06

Browse files
committed
Remove special case for type parameter
1 parent 81a0cf5 commit 1e2cb06

2 files changed

Lines changed: 31 additions & 43 deletions

File tree

crates/protosearch-plugin/src/mapping.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::collections::BTreeMap;
22
use std::fmt;
33

44
use protobuf::MessageDyn;
5-
use protobuf::reflect::{ReflectFieldRef, ReflectValueRef, RuntimeType};
5+
use protobuf::reflect::{
6+
FieldDescriptor, ReflectFieldRef, ReflectValueRef, RuntimeFieldType, RuntimeType,
7+
};
68
use serde::Serialize;
79
use serde_json::{Map, Value, json};
810

@@ -19,17 +21,10 @@ pub struct Mapping {
1921
#[serde(untagged)]
2022
pub 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+
97100
fn to_json(message: &dyn MessageDyn) -> Value {
98101
match message.descriptor_dyn().full_name() {
99102
"google.protobuf.Struct" => struct_to_json(message),

crates/protosearch-plugin/src/plugin.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,19 @@ fn compile_field(ctx: &Context, field: &FieldDescriptor) -> Result<Option<(Strin
6565
field: field.name().to_string(),
6666
source: e,
6767
})?;
68-
let Value::Object(mut params) = json else {
68+
let Value::Object(params) = json else {
6969
return Err(Error::InvalidJsonObject(field.name().into()));
7070
};
71-
let typ = params
72-
.remove("type")
73-
.and_then(|v| v.as_str().map(str::to_string))
74-
.unwrap_or_default();
75-
Property::Leaf {
76-
typ,
77-
parameters: params.into_iter().collect(),
78-
}
71+
Property::Leaf(params.into_iter().collect())
7972
}
8073
None => {
81-
let typ = if !options.field.type_().is_empty() {
82-
options.field.type_().to_string()
83-
} else {
84-
infer_type(field).to_string()
85-
};
86-
Property::from_options(&options.field, typ)
74+
let mut property = Property::from(&*options.field);
75+
if let Property::Leaf(ref mut parameters) = property {
76+
parameters
77+
.entry("type".into())
78+
.or_insert_with(|| Value::String(InferredType::from(field).to_string()));
79+
}
80+
property
8781
}
8882
};
8983
// A mapping type, as in an object or nested field.
@@ -96,8 +90,7 @@ fn compile_field(ctx: &Context, field: &FieldDescriptor) -> Result<Option<(Strin
9690
.transpose()?
9791
.unwrap_or_default();
9892
let property = match (mapping.properties.is_empty(), property) {
99-
(false, Property::Leaf { typ, parameters }) => Property::Mapping {
100-
typ,
93+
(false, Property::Leaf(parameters)) => Property::Mapping {
10194
parameters,
10295
properties: mapping,
10396
},
@@ -133,11 +126,3 @@ fn get_mapping_options(field: &FieldDescriptor) -> Result<Option<proto::Mapping>
133126
}
134127
Ok(if found { Some(mapping) } else { None })
135128
}
136-
137-
fn infer_type(field: &FieldDescriptor) -> InferredType {
138-
let rt = match field.runtime_field_type() {
139-
RuntimeFieldType::Singular(t) | RuntimeFieldType::Repeated(t) => t,
140-
RuntimeFieldType::Map(_, _) => return InferredType::Object,
141-
};
142-
InferredType::from(rt)
143-
}

0 commit comments

Comments
 (0)