NorfairKing / autodocodec

self(auto)- documenting encoders and decoders

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-line defaults

NorfairKing opened this issue · comments

This example from dnscheck:

data CheckSpec = CheckSpec
  { specRetryPolicy :: !RetryPolicySpec,
    specChecks :: ![Check]
  }
  deriving stock (Show, Eq, Generic)
  deriving (FromJSON, ToJSON) via (Autodocodec CheckSpec)

instance Validity CheckSpec

instance HasCodec CheckSpec where
  codec =
    object "CheckSpec" $
      CheckSpec
        <$> optionalFieldWithDefault "retry-policy" defaultRetryPolicySpec "The retry policy for flaky checks due to network failures etc" .= specRetryPolicy
        <*> requiredField "checks" "The checks to perform" .= specChecks

data RetryPolicySpec = RetryPolicySpec
  { retryPolicySpecMaxRetries :: !Word,
    retryPolicySpecBaseDelay :: !Word
  }defaultRetryPolicySpec :: RetryPolicySpec
defaultRetryPolicySpec =
  RetryPolicySpec
    { retryPolicySpecMaxRetries = 10,
      retryPolicySpecBaseDelay = 100_000 -- 100 ms
    }
  deriving stock (Show, Eq, Generic)
  deriving (FromJSON, ToJSON) via (Autodocodec RetryPolicySpec)

instance Validity RetryPolicySpec

instance HasCodec RetryPolicySpec where
  codec =
    object "RetryPolicySpec" $
      RetryPolicySpec
        <$> optionalFieldWithDefault "max-retries" (retryPolicySpecMaxRetries defaultRetryPolicySpec) "The maximum number of retries" .= retryPolicySpecMaxRetries
        <*> optionalFieldWithDefault "base-delay" (retryPolicySpecBaseDelay defaultRetryPolicySpec) "The delay between the first and second try, in microseconds" .= retryPolicySpecBaseDelay
defaultRetryPolicySpec :: RetryPolicySpec
defaultRetryPolicySpec =
  RetryPolicySpec
    { retryPolicySpecMaxRetries = 10,
      retryPolicySpecBaseDelay = 100_000 -- 100 ms
    }

Has a multi-line default value.
The resulting schema looks like this;
default