• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4020902227

pending completion
4020902227

Pull #743

github

GitHub
Merge 57279c6b6 into a12da35a5
Pull Request #743: Chore clippy fix

165 of 165 new or added lines in 60 files covered. (100.0%)

23638 of 35485 relevant lines covered (66.61%)

38417.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.44
/dozer-api/src/grpc/typed/helper.rs
1
use crate::grpc::types::{self as GrpcTypes};
2
use crate::grpc::types_helper::field_to_prost_value;
3
use dozer_types::types::Record;
4
use inflector::Inflector;
5
use prost_reflect::{DescriptorPool, MessageDescriptor};
6
use prost_reflect::{DynamicMessage, Value};
7

8
use super::TypedResponse;
9

10
fn get_response_descriptor(
8✔
11
    desc: &DescriptorPool,
8✔
12
    method: &str,
8✔
13
    endpoint_name: &str,
8✔
14
) -> MessageDescriptor {
8✔
15
    match method {
8✔
16
        "count" => {
8✔
17
            let count_path = format!(
3✔
18
                "dozer.generated.{}.Count{}Response",
3✔
19
                endpoint_name.to_lowercase(),
3✔
20
                endpoint_name.to_pascal_case().to_plural(),
3✔
21
            );
3✔
22

3✔
23
            desc.get_message_by_name(&count_path)
3✔
24
                .unwrap_or_else(|| panic!("{count_path}: not found"))
3✔
25
        }
26
        "query" => {
5✔
27
            let query_path = format!(
4✔
28
                "dozer.generated.{}.Query{}Response",
4✔
29
                endpoint_name.to_lowercase(),
4✔
30
                endpoint_name.to_pascal_case().to_plural(),
4✔
31
            );
4✔
32

4✔
33
            desc.get_message_by_name(&query_path)
4✔
34
                .unwrap_or_else(|| panic!("{query_path}: not found"))
4✔
35
        }
36
        "on_event" => {
1✔
37
            let query_path = format!(
1✔
38
                "dozer.generated.{}.{}Event",
1✔
39
                endpoint_name.to_lowercase(),
1✔
40
                endpoint_name.to_pascal_case().to_singular(),
1✔
41
            );
1✔
42

1✔
43
            desc.get_message_by_name(&query_path)
1✔
44
                .unwrap_or_else(|| panic!("{query_path}: not found"))
1✔
45
        }
46
        "token" => {
×
47
            let token_path = format!(
×
48
                "dozer.generated.{}.TokenResponse",
×
49
                endpoint_name.to_lowercase(),
×
50
            );
×
51
            desc.get_message_by_name(&token_path)
×
52
                .unwrap_or_else(|| panic!("{token_path}: not found"))
×
53
        }
54
        _ => panic!("method not found"),
×
55
    }
56
}
8✔
57

58
fn get_resource_desc(desc: &DescriptorPool, endpoint_name: &str) -> MessageDescriptor {
4✔
59
    let msg_path = format!(
4✔
60
        "dozer.generated.{}.{}",
4✔
61
        endpoint_name.to_lowercase(),
4✔
62
        endpoint_name.to_pascal_case().to_singular(),
4✔
63
    );
4✔
64

4✔
65
    desc.get_message_by_name(&msg_path)
4✔
66
        .unwrap_or_else(|| panic!("{msg_path}: not found"))
4✔
67
}
4✔
68

69
pub fn on_event_to_typed_response(
1✔
70
    op: GrpcTypes::Operation,
1✔
71
    desc: &DescriptorPool,
1✔
72
    endpoint_name: &str,
1✔
73
) -> TypedResponse {
1✔
74
    let event_desc = get_response_descriptor(desc, "on_event", endpoint_name);
1✔
75

1✔
76
    let mut event = DynamicMessage::new(event_desc);
1✔
77
    event.set_field_by_name("typ", prost_reflect::Value::EnumNumber(op.typ));
1✔
78
    if let Some(old) = op.old {
1✔
79
        event.set_field_by_name(
×
80
            "old",
×
81
            prost_reflect::Value::Message(internal_record_to_pb(old, desc, endpoint_name)),
×
82
        );
×
83
    }
1✔
84

85
    if let Some(new) = op.new {
1✔
86
        event.set_field_by_name(
1✔
87
            "new",
1✔
88
            prost_reflect::Value::Message(internal_record_to_pb(new, desc, endpoint_name)),
1✔
89
        );
1✔
90
    }
1✔
91

92
    TypedResponse::new(event)
1✔
93
}
1✔
94

95
fn internal_record_to_pb(
1✔
96
    rec: GrpcTypes::Record,
1✔
97
    desc: &DescriptorPool,
1✔
98
    endpoint_name: &str,
1✔
99
) -> DynamicMessage {
1✔
100
    let msg_path = format!(
1✔
101
        "dozer.generated.{}.{}",
1✔
102
        endpoint_name.to_lowercase(),
1✔
103
        endpoint_name.to_pascal_case().to_singular(),
1✔
104
    );
1✔
105
    let resource_desc = desc
1✔
106
        .get_message_by_name(&msg_path)
1✔
107
        .unwrap_or_else(|| panic!("{msg_path}: not found"));
1✔
108
    let mut resource = DynamicMessage::new(resource_desc.to_owned());
1✔
109

110
    for (field, value) in resource_desc.fields().zip(rec.values.into_iter()) {
4✔
111
        if let Some(value) = interval_value_to_pb(value) {
4✔
112
            resource.set_field(&field, value);
2✔
113
        }
2✔
114
    }
115
    resource
1✔
116
}
1✔
117

118
fn interval_value_to_pb(value: GrpcTypes::Value) -> Option<prost_reflect::Value> {
420✔
119
    value.value.map(|value| match value {
420✔
120
        GrpcTypes::value::Value::UintValue(n) => Value::U64(n),
209✔
121
        GrpcTypes::value::Value::IntValue(n) => Value::I64(n),
×
122
        GrpcTypes::value::Value::FloatValue(n) => Value::F32(n),
×
123
        GrpcTypes::value::Value::BoolValue(n) => Value::Bool(n),
×
124
        GrpcTypes::value::Value::StringValue(n) => Value::String(n),
105✔
125
        GrpcTypes::value::Value::BytesValue(n) => {
×
126
            Value::Bytes(prost_reflect::bytes::Bytes::from(n))
×
127
        }
128
        GrpcTypes::value::Value::DoubleValue(n) => Value::F64(n),
×
129
        _ => todo!(),
×
130
    })
420✔
131
}
420✔
132
fn record_to_pb(record: Record, desc: &MessageDescriptor) -> DynamicMessage {
104✔
133
    let mut resource = DynamicMessage::new(desc.clone());
104✔
134
    for (field, value) in desc.fields().zip(record.values.into_iter()) {
416✔
135
        if let Some(value) = interval_value_to_pb(field_to_prost_value(value)) {
416✔
136
            resource.set_field(&field, value);
312✔
137
        }
312✔
138
    }
139
    resource
104✔
140
}
104✔
141

142
pub fn count_response_to_typed_response(
3✔
143
    count: usize,
3✔
144
    desc: &DescriptorPool,
3✔
145
    endpoint_name: &str,
3✔
146
) -> TypedResponse {
3✔
147
    let count_desc = get_response_descriptor(desc, "count", endpoint_name);
3✔
148

3✔
149
    let mut msg = DynamicMessage::new(count_desc);
3✔
150
    msg.set_field_by_name("count", prost_reflect::Value::U64(count as _));
3✔
151

3✔
152
    TypedResponse::new(msg)
3✔
153
}
3✔
154

155
pub fn query_response_to_typed_response(
4✔
156
    records: Vec<Record>,
4✔
157
    desc: &DescriptorPool,
4✔
158
    endpoint_name: &str,
4✔
159
) -> TypedResponse {
4✔
160
    let query_desc = get_response_descriptor(desc, "query", endpoint_name);
4✔
161

4✔
162
    let mut msg = DynamicMessage::new(query_desc);
4✔
163

4✔
164
    let resource_desc = get_resource_desc(desc, endpoint_name);
4✔
165
    let resources = records
4✔
166
        .into_iter()
4✔
167
        .map(|rec| prost_reflect::Value::Message(record_to_pb(rec, &resource_desc)))
104✔
168
        .collect::<Vec<_>>();
4✔
169
    msg.set_field_by_name("data", prost_reflect::Value::List(resources));
4✔
170
    TypedResponse::new(msg)
4✔
171
}
4✔
172

173
pub fn token_response(token: String, desc: &DescriptorPool, endpoint_name: &str) -> TypedResponse {
×
174
    let token_desc = get_response_descriptor(desc, "token", endpoint_name);
×
175
    let mut msg = DynamicMessage::new(token_desc);
×
176
    msg.set_field_by_name("token", prost_reflect::Value::String(token));
×
177
    TypedResponse::new(msg)
×
178
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc