Fix display of null and missing defaults and examples (#457)

* flake-info: fix import of `null` defaults and examples

Currently they're being treated as missing fields, and hence displayed
as "Not given" on the frontend.

* Bump VERSION

* frontend/Options: don't show missing fields

Co-authored-by: Rok Garbas <rok@garbas.si>
This commit is contained in:
Naïm Favier 2022-04-07 23:57:35 +02:00 committed by GitHub
parent 808e929a71
commit ff22728d9a
Failed to generate hash of commit
3 changed files with 63 additions and 53 deletions

View file

@ -1 +1 @@
28
29

View file

@ -61,7 +61,11 @@ pub struct NixOption {
#[serde(rename = "type")]
/// Nix generated description of the options type
pub option_type: Option<String>,
#[serde(deserialize_with = "optional_field", default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<DocValue>,
#[serde(deserialize_with = "optional_field", default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub example: Option<DocValue>,
/// If defined in a flake, contains defining flake and optionally a module
@ -89,9 +93,9 @@ pub enum DocValue {
#[serde(tag = "_type", content = "text")]
pub enum Literal {
#[serde(rename = "literalExpression")]
LiteralExpression(Value),
LiteralExpression(String),
#[serde(rename = "literalExample")]
LiteralExample(Value),
LiteralExample(String),
#[serde(rename = "literalDocBook")]
LiteralDocBook(String),
}
@ -103,7 +107,7 @@ impl Serialize for DocValue {
{
match self {
DocValue::Literal(Literal::LiteralExample(s) | Literal::LiteralExpression(s)) => {
return serializer.serialize_some(&s);
return serializer.serialize_str(&s);
}
DocValue::Literal(Literal::LiteralDocBook(doc_book)) => {
return serializer.serialize_str(&doc_book.render().unwrap_or_else(|e| {
@ -295,6 +299,16 @@ where
deserializer.deserialize_any(StringOrStruct(PhantomData))
}
/// Deserializes an Option<T> by passing `null` along to T's deserializer instead
/// of treating it as a missing field
fn optional_field<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
Ok(Some(T::deserialize(deserializer)?))
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;

View file

@ -178,63 +178,59 @@ viewResultItem channel _ show item =
Err _ ->
Nothing
default =
"Not given"
asPre value =
pre [] [ text value ]
asPreCode value =
div [] [ pre [] [ code [ class "code-block" ] [ text value ] ] ]
withEmpty wrapWith maybe =
case maybe of
Nothing ->
asPre default
Just "" ->
asPre default
Just value ->
wrapWith value
wrapped wrapWith value =
case value of
"" ->
wrapWith <| "\"" ++ value ++ "\""
_ ->
wrapWith value
showDetails =
if Just item.source.name == show then
div [ Html.Attributes.map SearchMsg Search.trapClick ]
Just <|
div [ Html.Attributes.map SearchMsg Search.trapClick ] <|
[ div [] [ text "Name" ]
, div [] [ wrapped asPreCode item.source.name ]
, div [] [ text "Description" ]
, div [] <|
(item.source.description
, div [] [ asPreCode item.source.name ]
]
++ (item.source.description
|> Maybe.andThen showHtml
|> Maybe.map
(\description ->
[ div [] [ text "Description" ]
, div [] description
]
)
|> Maybe.withDefault []
)
, div [] [ text "Default value" ]
, div [] <|
(item.source.default
|> Maybe.map (\value -> Maybe.withDefault [ asPreCode value ] (showHtml value))
|> Maybe.withDefault [ asPre default ]
++ (item.source.type_
|> Maybe.map
(\type_ ->
[ div [] [ text "Type" ]
, div [] [ asPre type_ ]
]
)
, div [] [ text "Type" ]
, div [] [ withEmpty asPre item.source.type_ ]
, div [] [ text "Example" ]
, div [] <|
(item.source.example
|> Maybe.map (\value -> Maybe.withDefault [ asPreCode value ] (showHtml value))
|> Maybe.withDefault [ asPre default ]
|> Maybe.withDefault []
)
, div [] [ text "Declared in" ]
++ (item.source.default
|> Maybe.map
(\default ->
[ div [] [ text "Default" ]
, div [] <| Maybe.withDefault [ asPreCode default ] (showHtml default)
]
)
|> Maybe.withDefault []
)
++ (item.source.example
|> Maybe.map
(\example ->
[ div [] [ text "Example" ]
, div [] <| Maybe.withDefault [ asPreCode example ] (showHtml example)
]
)
|> Maybe.withDefault []
)
++ [ div [] [ text "Declared in" ]
, div [] <| findSource channel item.source
]
|> Just
else
Nothing