From 1d0a9de7da98bd1614c727be1de4227e11b774ec Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 18:24:41 +0200 Subject: [PATCH] Automate defaultToString behaviour --- src/Internal/Filter/Timeline.elm | 2 -- src/Internal/Tools/Iddict.elm | 1 - src/Internal/Tools/Json.elm | 12 ++++++------ src/Internal/Values/Context.elm | 1 - src/Internal/Values/Envelope.elm | 1 - src/Internal/Values/Room.elm | 5 ----- src/Internal/Values/Settings.elm | 10 ---------- src/Internal/Values/Timeline.elm | 5 ----- tests/Test/Tools/Json.elm | 9 --------- 9 files changed, 6 insertions(+), 40 deletions(-) diff --git a/src/Internal/Filter/Timeline.elm b/src/Internal/Filter/Timeline.elm index 71e59d8..9b9d6c5 100644 --- a/src/Internal/Filter/Timeline.elm +++ b/src/Internal/Filter/Timeline.elm @@ -183,7 +183,6 @@ coder = , description = Text.fields.timelineFilter.senders , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.required @@ -199,7 +198,6 @@ coder = , description = Text.fields.timelineFilter.types , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.required diff --git a/src/Internal/Tools/Iddict.elm b/src/Internal/Tools/Iddict.elm index da718f2..6c99363 100644 --- a/src/Internal/Tools/Iddict.elm +++ b/src/Internal/Tools/Iddict.elm @@ -80,7 +80,6 @@ coder x = , description = Text.fields.iddict.cursor , coder = Json.int , default = ( 0, [] ) - , defaultToString = String.fromInt } ) (Json.field.required diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index 4962d45..3ab93b3 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -362,7 +362,7 @@ then the following field type would be used: , 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 field type: @@ -373,8 +373,7 @@ field type: [ "The hobbies of the person. Can be omitted." ] , coder = list string - , default = ( [], [] ) -- 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 + , default = ( [ "football" ], [] ) -- The `List Log` can be inserted in case you wish to insert a message when relying on a default } -} @@ -382,7 +381,7 @@ field : { required : { fieldName : String, toField : object -> a, description : List String, coder : Coder a } -> Field a object , optional : { 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 = @@ -425,7 +424,7 @@ field = , requiredness = OptionalField } , withDefault = - \{ fieldName, toField, description, coder, default, defaultToString } -> + \{ fieldName, toField, description, coder, default } -> case coder of Coder { encoder, decoder, docs } -> Field @@ -449,7 +448,8 @@ field = , requiredness = default |> Tuple.first - |> defaultToString + |> encoder + |> E.encode 0 |> OptionalFieldWithDefault } } diff --git a/src/Internal/Values/Context.elm b/src/Internal/Values/Context.elm index 0449e7c..ca6a8a1 100644 --- a/src/Internal/Values/Context.elm +++ b/src/Internal/Values/Context.elm @@ -439,6 +439,5 @@ versionsCoder = , description = Text.fields.versions.unstableFeatures , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = Json.encode (Json.set Json.string) >> E.encode 0 } ) diff --git a/src/Internal/Values/Envelope.elm b/src/Internal/Values/Envelope.elm index 7823e62..7ae8ceb 100644 --- a/src/Internal/Values/Envelope.elm +++ b/src/Internal/Values/Envelope.elm @@ -124,7 +124,6 @@ coder c1 = , description = Text.fields.envelope.settings , coder = Settings.coder , default = Tuple.pair Settings.init [] - , defaultToString = always "" } ) diff --git a/src/Internal/Values/Room.elm b/src/Internal/Values/Room.elm index 6f5c198..2dd93b9 100644 --- a/src/Internal/Values/Room.elm +++ b/src/Internal/Values/Room.elm @@ -155,7 +155,6 @@ coder = , description = Text.fields.room.accountData , coder = Json.fastDict Json.value , default = ( Dict.empty, [] ) - , defaultToString = Json.encode (Json.fastDict Json.value) >> E.encode 0 } ) (Json.field.optional.withDefault @@ -164,7 +163,6 @@ coder = , description = Text.fields.room.ephemeral , coder = Json.list StrippedEvent.coder , default = ( [], [] ) - , defaultToString = Json.encode (Json.list StrippedEvent.coder) >> E.encode 0 } ) (Json.field.optional.withDefault @@ -173,7 +171,6 @@ coder = , description = Text.fields.room.events , coder = Hashdict.coder .eventId Event.coder , 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 @@ -189,7 +186,6 @@ coder = , description = Text.fields.room.state , coder = StateManager.coder , default = ( StateManager.empty, [] ) - , defaultToString = Json.encode StateManager.coder >> E.encode 0 } ) (Json.field.optional.withDefault @@ -198,7 +194,6 @@ coder = , description = Text.fields.room.timeline , coder = Timeline.coder , default = ( Timeline.empty, [] ) - , defaultToString = Json.encode Timeline.coder >> E.encode 0 } ) diff --git a/src/Internal/Values/Settings.elm b/src/Internal/Values/Settings.elm index eed039a..d0f40c4 100644 --- a/src/Internal/Values/Settings.elm +++ b/src/Internal/Values/Settings.elm @@ -55,7 +55,6 @@ coder = , description = Text.fields.settings.currentVersion , coder = Json.string , default = Tuple.pair Default.currentVersion [] - , defaultToString = identity } ) (Json.field.optional.withDefault @@ -64,7 +63,6 @@ coder = , description = Text.fields.settings.deviceName , coder = Json.string , default = Tuple.pair Default.deviceName [] - , defaultToString = identity } ) (Json.field.optional.withDefault @@ -73,13 +71,6 @@ coder = , description = Text.fields.settings.removePasswordOnLogin , coder = Json.bool , default = Tuple.pair Default.removePasswordOnLogin [] - , defaultToString = - \b -> - if b then - "true" - - else - "false" } ) (Json.field.optional.withDefault @@ -88,7 +79,6 @@ coder = , description = Text.fields.settings.syncTime , coder = Json.int , default = Tuple.pair Default.syncTime [] - , defaultToString = String.fromInt } ) diff --git a/src/Internal/Values/Timeline.elm b/src/Internal/Values/Timeline.elm index 9bded76..548b1a1 100644 --- a/src/Internal/Values/Timeline.elm +++ b/src/Internal/Values/Timeline.elm @@ -226,7 +226,6 @@ coder = , description = Text.fields.timeline.filledBatches , coder = Json.int , default = ( 0, [] ) - , defaultToString = String.fromInt } ) (Json.field.required @@ -326,7 +325,6 @@ coderIToken = , description = Text.fields.itoken.starts , coder = Json.set coderIBatchPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -335,7 +333,6 @@ coderIToken = , description = Text.fields.itoken.ends , coder = Json.set coderIBatchPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -344,7 +341,6 @@ coderIToken = , description = Text.fields.itoken.inFrontOf , coder = Json.set coderITokenPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -353,7 +349,6 @@ coderIToken = , description = Text.fields.itoken.behind , coder = Json.set coderITokenPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) diff --git a/tests/Test/Tools/Json.elm b/tests/Test/Tools/Json.elm index 6124615..13d4c3a 100644 --- a/tests/Test/Tools/Json.elm +++ b/tests/Test/Tools/Json.elm @@ -100,7 +100,6 @@ gridField = , description = [] , coder = Json.list (Json.list Json.int) , default = ( [], [] ) - , defaultToString = always "[]" } @@ -132,7 +131,6 @@ hobbiesField = , description = [] , coder = Json.list Json.string , default = ( [], [] ) - , defaultToString = always "[]" } @@ -149,13 +147,6 @@ invitedToPartyField = , description = [] , coder = Json.bool , default = ( False, [] ) - , defaultToString = - \b -> - if b then - "True" - - else - "False" }