-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model: change
container-registry.mirrors
type
Changes the model for `container-registry.mirrors` from a `HashMap` to a vector of structs. Adds a custom deserializer to deserialize from either a TOML table or a TOML sequence.
- Loading branch information
Showing
8 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{{#if settings.container-registry.mirrors}} | ||
{{#each settings.container-registry.mirrors}} | ||
[mirrors."{{@key}}"] | ||
endpoints = [{{join_array ", " this }}] | ||
[mirrors."{{registry}}"] | ||
endpoints = [{{join_array ", " endpoint }}] | ||
{{/each}} | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::RegistryMirror; | ||
use serde::de::value::SeqAccessDeserializer; | ||
use serde::de::{MapAccess, SeqAccess, Visitor}; | ||
use serde::{Deserialize, Deserializer}; | ||
use std::fmt::Formatter; | ||
|
||
// Our standard representation of registry mirrors is a `Vec` of `RegistryMirror`; for backward compatibility, we also allow a `HashMap` of registry to endpoints. | ||
pub(crate) fn deserialize_mirrors<'de, D>( | ||
deserializer: D, | ||
) -> Result<Option<Vec<RegistryMirror>>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct TableOrArray; | ||
|
||
impl<'de> Visitor<'de> for TableOrArray { | ||
type Value = Option<Vec<RegistryMirror>>; | ||
|
||
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { | ||
formatter.write_str("TOML array or TOML table") | ||
} | ||
|
||
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: SeqAccess<'de>, | ||
{ | ||
Ok(Some(Deserialize::deserialize(SeqAccessDeserializer::new( | ||
seq, | ||
))?)) | ||
} | ||
|
||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> | ||
where | ||
M: MapAccess<'de>, | ||
{ | ||
let mut vec = Vec::new(); | ||
while let Some((k, v)) = map.next_entry()? { | ||
vec.push(RegistryMirror { | ||
registry: Some(k), | ||
endpoint: Some(v), | ||
}); | ||
} | ||
Ok(Some(vec)) | ||
} | ||
} | ||
deserializer.deserialize_any(TableOrArray) | ||
} | ||
|
||
#[cfg(test)] | ||
mod mirrors_tests { | ||
use crate::RegistrySettings; | ||
static TEST_MIRRORS_ARRAY: &str = include_str!("../tests/data/mirrors-array"); | ||
static TEST_MIRRORS_TABLE: &str = include_str!("../tests/data/mirrors-table"); | ||
|
||
#[test] | ||
fn registry_mirrors_array_representation() { | ||
assert!(toml::from_str::<RegistrySettings>(TEST_MIRRORS_ARRAY).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn registry_mirrors_table_representation() { | ||
assert!(toml::from_str::<RegistrySettings>(TEST_MIRRORS_TABLE).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn representation_equal() { | ||
assert_eq!( | ||
toml::from_str::<RegistrySettings>(TEST_MIRRORS_TABLE).unwrap(), | ||
toml::from_str::<RegistrySettings>(TEST_MIRRORS_ARRAY).unwrap() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[[mirrors]] | ||
registry = "*" | ||
endpoint = ["hello"] | ||
|
||
[[mirrors]] | ||
registry = "example" | ||
endpoint = [ "hello", "hellohello"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[mirrors] | ||
"*" = ["hello"] | ||
"example" = ["hello", "hellohello"] |