Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deserialization to SocketAddr struct to make port for dns_config optional #1564

Merged
merged 14 commits into from
Mar 28, 2024
44 changes: 37 additions & 7 deletions crates/ott-common/src/discovery/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ fn deserialize_dns_server<'de, D>(deserializer: D) -> Result<Option<SocketAddr>,
where
D: Deserializer<'de>,
{
let mut buf = String::deserialize(deserializer)?;
let buf = String::deserialize(deserializer)?;

if Option::is_none(&buf.find(':')) {
buf.push_str(":53");
if buf.is_empty() {
return Ok(None);
}

Ok(Some(
SocketAddr::from_str(&buf).expect("Error: Could not unwrap"),
))
if IpAddr::from_str(&buf).is_ok() {
Ok(Some(SocketAddr::new(IpAddr::from_str(&buf).unwrap(), 53)))
cjrkoa marked this conversation as resolved.
Show resolved Hide resolved
} else {
Ok(Some(SocketAddr::from_str(&buf).unwrap()))
cjrkoa marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub struct DnsServiceDiscoverer {
Expand Down Expand Up @@ -95,7 +97,21 @@ mod test {
use serde_json::json;

#[test]
fn server_deserializes_correctly() {
fn dns_server_deserializes_correctly() {
let json = json!({
"service_port": 8080,
"dns_server": "127.0.0.1:100",
"query": "".to_string(),
});

let config: DnsDiscoveryConfig =
serde_json::from_value(json).expect("Failed to deserialize json");

assert_eq!(config.dns_server, Some(([127, 0, 0, 1], 100).into()));
}

#[test]
fn dns_server_deserialization_defaults_to_port_53() {
let json = json!({
"service_port": 8080,
"dns_server": "127.0.0.1",
Expand All @@ -107,4 +123,18 @@ mod test {

assert_eq!(config.dns_server, Some(([127, 0, 0, 1], 53).into()));
}

#[test]
fn dns_server_is_optional() {
let json = json!({
"service_port": 8080,
"dns_server": "",
cjrkoa marked this conversation as resolved.
Show resolved Hide resolved
"query": "".to_string(),
});

let config: DnsDiscoveryConfig =
serde_json::from_value(json).expect("Failed to deserialize json");

assert_eq!(config.dns_server, None);
}
}
Loading