Automate defaultToString behaviour

pull/31/head
Bram 2024-07-08 18:24:41 +02:00
parent 95f0aa2934
commit 1d0a9de7da
9 changed files with 6 additions and 40 deletions

View File

@ -183,7 +183,6 @@ coder =
, description = Text.fields.timelineFilter.senders , description = Text.fields.timelineFilter.senders
, coder = Json.set Json.string , coder = Json.set Json.string
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )
(Json.field.required (Json.field.required
@ -199,7 +198,6 @@ coder =
, description = Text.fields.timelineFilter.types , description = Text.fields.timelineFilter.types
, coder = Json.set Json.string , coder = Json.set Json.string
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )
(Json.field.required (Json.field.required

View File

@ -80,7 +80,6 @@ coder x =
, description = Text.fields.iddict.cursor , description = Text.fields.iddict.cursor
, coder = Json.int , coder = Json.int
, default = ( 0, [] ) , default = ( 0, [] )
, defaultToString = String.fromInt
} }
) )
(Json.field.required (Json.field.required

View File

@ -362,7 +362,7 @@ then the following field type would be used:
, coder = string , coder = string
} }
Suppose the JSO isn't obligated to provide a list of hobbies, and the list would Suppose the JSON isn't obligated to provide a list of hobbies, and the list would
by default be overriden with an empty list, then we would use the following by default be overriden with an empty list, then we would use the following
field type: field type:
@ -373,8 +373,7 @@ field type:
[ "The hobbies of the person. Can be omitted." [ "The hobbies of the person. Can be omitted."
] ]
, coder = list string , coder = list string
, default = ( [], [] ) -- The `List Log` can be inserted in case you wish to insert a message when relying on a default , default = ( [ "football" ], [] ) -- The `List Log` can be inserted in case you wish to insert a message when relying on a default
, defaultToString = always "[]" -- Default converted to a string
} }
-} -}
@ -382,7 +381,7 @@ field :
{ required : { fieldName : String, toField : object -> a, description : List String, coder : Coder a } -> Field a object { required : { fieldName : String, toField : object -> a, description : List String, coder : Coder a } -> Field a object
, optional : , optional :
{ value : { fieldName : String, toField : object -> Maybe a, description : List String, coder : Coder a } -> Field (Maybe a) object { value : { fieldName : String, toField : object -> Maybe a, description : List String, coder : Coder a } -> Field (Maybe a) object
, withDefault : { fieldName : String, toField : object -> a, description : List String, coder : Coder a, default : ( a, List Log ), defaultToString : a -> String } -> Field a object , withDefault : { fieldName : String, toField : object -> a, description : List String, coder : Coder a, default : ( a, List Log ) } -> Field a object
} }
} }
field = field =
@ -425,7 +424,7 @@ field =
, requiredness = OptionalField , requiredness = OptionalField
} }
, withDefault = , withDefault =
\{ fieldName, toField, description, coder, default, defaultToString } -> \{ fieldName, toField, description, coder, default } ->
case coder of case coder of
Coder { encoder, decoder, docs } -> Coder { encoder, decoder, docs } ->
Field Field
@ -449,7 +448,8 @@ field =
, requiredness = , requiredness =
default default
|> Tuple.first |> Tuple.first
|> defaultToString |> encoder
|> E.encode 0
|> OptionalFieldWithDefault |> OptionalFieldWithDefault
} }
} }

View File

@ -439,6 +439,5 @@ versionsCoder =
, description = Text.fields.versions.unstableFeatures , description = Text.fields.versions.unstableFeatures
, coder = Json.set Json.string , coder = Json.set Json.string
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = Json.encode (Json.set Json.string) >> E.encode 0
} }
) )

View File

@ -124,7 +124,6 @@ coder c1 =
, description = Text.fields.envelope.settings , description = Text.fields.envelope.settings
, coder = Settings.coder , coder = Settings.coder
, default = Tuple.pair Settings.init [] , default = Tuple.pair Settings.init []
, defaultToString = always "<Default settings>"
} }
) )

View File

@ -155,7 +155,6 @@ coder =
, description = Text.fields.room.accountData , description = Text.fields.room.accountData
, coder = Json.fastDict Json.value , coder = Json.fastDict Json.value
, default = ( Dict.empty, [] ) , default = ( Dict.empty, [] )
, defaultToString = Json.encode (Json.fastDict Json.value) >> E.encode 0
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -164,7 +163,6 @@ coder =
, description = Text.fields.room.ephemeral , description = Text.fields.room.ephemeral
, coder = Json.list StrippedEvent.coder , coder = Json.list StrippedEvent.coder
, default = ( [], [] ) , default = ( [], [] )
, defaultToString = Json.encode (Json.list StrippedEvent.coder) >> E.encode 0
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -173,7 +171,6 @@ coder =
, description = Text.fields.room.events , description = Text.fields.room.events
, coder = Hashdict.coder .eventId Event.coder , coder = Hashdict.coder .eventId Event.coder
, default = ( Hashdict.empty .eventId, [ log.warn "Found a room with no known events! Is it empty?" ] ) , default = ( Hashdict.empty .eventId, [ log.warn "Found a room with no known events! Is it empty?" ] )
, defaultToString = Json.encode (Hashdict.coder .eventId Event.coder) >> E.encode 0
} }
) )
(Json.field.required (Json.field.required
@ -189,7 +186,6 @@ coder =
, description = Text.fields.room.state , description = Text.fields.room.state
, coder = StateManager.coder , coder = StateManager.coder
, default = ( StateManager.empty, [] ) , default = ( StateManager.empty, [] )
, defaultToString = Json.encode StateManager.coder >> E.encode 0
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -198,7 +194,6 @@ coder =
, description = Text.fields.room.timeline , description = Text.fields.room.timeline
, coder = Timeline.coder , coder = Timeline.coder
, default = ( Timeline.empty, [] ) , default = ( Timeline.empty, [] )
, defaultToString = Json.encode Timeline.coder >> E.encode 0
} }
) )

View File

@ -55,7 +55,6 @@ coder =
, description = Text.fields.settings.currentVersion , description = Text.fields.settings.currentVersion
, coder = Json.string , coder = Json.string
, default = Tuple.pair Default.currentVersion [] , default = Tuple.pair Default.currentVersion []
, defaultToString = identity
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -64,7 +63,6 @@ coder =
, description = Text.fields.settings.deviceName , description = Text.fields.settings.deviceName
, coder = Json.string , coder = Json.string
, default = Tuple.pair Default.deviceName [] , default = Tuple.pair Default.deviceName []
, defaultToString = identity
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -73,13 +71,6 @@ coder =
, description = Text.fields.settings.removePasswordOnLogin , description = Text.fields.settings.removePasswordOnLogin
, coder = Json.bool , coder = Json.bool
, default = Tuple.pair Default.removePasswordOnLogin [] , default = Tuple.pair Default.removePasswordOnLogin []
, defaultToString =
\b ->
if b then
"true"
else
"false"
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -88,7 +79,6 @@ coder =
, description = Text.fields.settings.syncTime , description = Text.fields.settings.syncTime
, coder = Json.int , coder = Json.int
, default = Tuple.pair Default.syncTime [] , default = Tuple.pair Default.syncTime []
, defaultToString = String.fromInt
} }
) )

View File

@ -226,7 +226,6 @@ coder =
, description = Text.fields.timeline.filledBatches , description = Text.fields.timeline.filledBatches
, coder = Json.int , coder = Json.int
, default = ( 0, [] ) , default = ( 0, [] )
, defaultToString = String.fromInt
} }
) )
(Json.field.required (Json.field.required
@ -326,7 +325,6 @@ coderIToken =
, description = Text.fields.itoken.starts , description = Text.fields.itoken.starts
, coder = Json.set coderIBatchPTRValue , coder = Json.set coderIBatchPTRValue
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -335,7 +333,6 @@ coderIToken =
, description = Text.fields.itoken.ends , description = Text.fields.itoken.ends
, coder = Json.set coderIBatchPTRValue , coder = Json.set coderIBatchPTRValue
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -344,7 +341,6 @@ coderIToken =
, description = Text.fields.itoken.inFrontOf , description = Text.fields.itoken.inFrontOf
, coder = Json.set coderITokenPTRValue , coder = Json.set coderITokenPTRValue
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )
(Json.field.optional.withDefault (Json.field.optional.withDefault
@ -353,7 +349,6 @@ coderIToken =
, description = Text.fields.itoken.behind , description = Text.fields.itoken.behind
, coder = Json.set coderITokenPTRValue , coder = Json.set coderITokenPTRValue
, default = ( Set.empty, [] ) , default = ( Set.empty, [] )
, defaultToString = always "[]"
} }
) )

View File

@ -100,7 +100,6 @@ gridField =
, description = [] , description = []
, coder = Json.list (Json.list Json.int) , coder = Json.list (Json.list Json.int)
, default = ( [], [] ) , default = ( [], [] )
, defaultToString = always "[]"
} }
@ -132,7 +131,6 @@ hobbiesField =
, description = [] , description = []
, coder = Json.list Json.string , coder = Json.list Json.string
, default = ( [], [] ) , default = ( [], [] )
, defaultToString = always "[]"
} }
@ -149,13 +147,6 @@ invitedToPartyField =
, description = [] , description = []
, coder = Json.bool , coder = Json.bool
, default = ( False, [] ) , default = ( False, [] )
, defaultToString =
\b ->
if b then
"True"
else
"False"
} }