Is there a key-transformer, that even if the schema shows the key as a string, it’ll keyword it anyway?
(mt/key-transformer {:decode keyword})
My code doesn’t seem to get the change to change keys to strings before stripping extra keys. Does this have to be a decode instead?
(mt/transformer (mt/key-transformer {:encode name})
mt/strip-extra-keys-transformer)@joel.kaasinen I’m wondering if I’m going about this correctly, I have a timestamp for some reason stored as a string
["timestamp" {:decode/string malli.transform/-string->long :encode/string str} :string]
I wan to work with it as a long, the above works, but maybe a cleaner way?like having a :timestamp type or somesuch.
yeah if you want to be a bit more structured about it you could either:
• A) add :timestamp [:string {:decode/string ... :encode/string ...}] to your registry
• B) add :timestamp :string to your registry
◦ and add a (transformer {:name :timestamp-trans :decoders {:timestamp ...} :encoders {:timestamp ...}}) to your transformer pipeline (in addition or instead of string-transformer)
there's one more way:
(def Timestamp [:string {:decode/string ... :encode/string ...}])
[:map ["timestamp" Timestamp]]I'm having a hard time following your question. Could you give some examples of what's happening now and what you would want to happen?
Meanwhile, this might help:
• mt/strip-extra-keys-transformer always works in the :enter phase of transforming
• mt/key-transformer works in the :leave phase when encoding, :enter when decoding
• This means that when encoding, strip-extra-keys always runs before key-transformer
• ... and when decoding, you can choose the order
Maybe this is what you want?
(m/decode [:map ["foo1" :string]]
{"foo" "x" "bar" "y"}
(mt/transformer (mt/key-transformer {:decode #(str % "1")})
mt/strip-extra-keys-transformer))
; ==> {"foo1" "x"}"foo" gets decoded to "foo1", which then doesn't get stripped by strip-extra-keys-transformer
Thanks, knowing that it can work on decoding helps.