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")] #[serde(rename = "type")]
/// Nix generated description of the options type /// Nix generated description of the options type
pub option_type: Option<String>, pub option_type: Option<String>,
#[serde(deserialize_with = "optional_field", default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<DocValue>, pub default: Option<DocValue>,
#[serde(deserialize_with = "optional_field", default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub example: Option<DocValue>, pub example: Option<DocValue>,
/// If defined in a flake, contains defining flake and optionally a module /// If defined in a flake, contains defining flake and optionally a module
@ -89,9 +93,9 @@ pub enum DocValue {
#[serde(tag = "_type", content = "text")] #[serde(tag = "_type", content = "text")]
pub enum Literal { pub enum Literal {
#[serde(rename = "literalExpression")] #[serde(rename = "literalExpression")]
LiteralExpression(Value), LiteralExpression(String),
#[serde(rename = "literalExample")] #[serde(rename = "literalExample")]
LiteralExample(Value), LiteralExample(String),
#[serde(rename = "literalDocBook")] #[serde(rename = "literalDocBook")]
LiteralDocBook(String), LiteralDocBook(String),
} }
@ -103,7 +107,7 @@ impl Serialize for DocValue {
{ {
match self { match self {
DocValue::Literal(Literal::LiteralExample(s) | Literal::LiteralExpression(s)) => { DocValue::Literal(Literal::LiteralExample(s) | Literal::LiteralExpression(s)) => {
return serializer.serialize_some(&s); return serializer.serialize_str(&s);
} }
DocValue::Literal(Literal::LiteralDocBook(doc_book)) => { DocValue::Literal(Literal::LiteralDocBook(doc_book)) => {
return serializer.serialize_str(&doc_book.render().unwrap_or_else(|e| { return serializer.serialize_str(&doc_book.render().unwrap_or_else(|e| {
@ -295,6 +299,16 @@ where
deserializer.deserialize_any(StringOrStruct(PhantomData)) 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)] #[cfg(test)]
mod tests { mod tests {
use std::collections::HashMap; use std::collections::HashMap;

View file

@ -178,63 +178,59 @@ viewResultItem channel _ show item =
Err _ -> Err _ ->
Nothing Nothing
default =
"Not given"
asPre value = asPre value =
pre [] [ text value ] pre [] [ text value ]
asPreCode value = asPreCode value =
div [] [ pre [] [ code [ class "code-block" ] [ text 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 = showDetails =
if Just item.source.name == show then 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 [] [ text "Name" ]
, div [] [ wrapped asPreCode item.source.name ] , div [] [ asPreCode item.source.name ]
, div [] [ text "Description" ] ]
, div [] <| ++ (item.source.description
(item.source.description
|> Maybe.andThen showHtml |> Maybe.andThen showHtml
|> Maybe.map
(\description ->
[ div [] [ text "Description" ]
, div [] description
]
)
|> Maybe.withDefault [] |> Maybe.withDefault []
) )
, div [] [ text "Default value" ] ++ (item.source.type_
, div [] <| |> Maybe.map
(item.source.default (\type_ ->
|> Maybe.map (\value -> Maybe.withDefault [ asPreCode value ] (showHtml value)) [ div [] [ text "Type" ]
|> Maybe.withDefault [ asPre default ] , div [] [ asPre type_ ]
]
) )
, div [] [ text "Type" ] |> Maybe.withDefault []
, 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 ]
) )
, 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 , div [] <| findSource channel item.source
] ]
|> Just
else else
Nothing Nothing