@@ -36,6 +36,7 @@ import Data.Avro qualified as Avro
36
36
import Data.Avro.Encoding.FromAvro qualified as FromAvro
37
37
import Data.Avro.Schema.ReadSchema qualified as ReadSchema
38
38
import Data.Avro.Schema.Schema qualified as AvroSchema
39
+ import Data.Binary.Get qualified as Get
39
40
import Data.Bitraversable (bimapM )
40
41
import Data.Bits (shiftL , shiftR , (.|.) )
41
42
import Data.ByteArray qualified as BA
@@ -948,6 +949,8 @@ foreignCallHelper = \case
948
949
errv = encodeJsonParseError err
949
950
Json_tryUnconsText -> mkForeign $ \ (txt :: Text ) ->
950
951
pure . bimap encodeJsonParseError (second encodeVal) $ parseJson txt
952
+ Avro_decodeBinary -> mkForeign $ \ (env :: Closure , readSchema :: Closure , bytes :: Bytes. Bytes ) -> do
953
+ avroDecodeBinary env readSchema bytes
951
954
where
952
955
forceListSpine xs = foldl (\ u x -> x `seq` u) xs xs
953
956
chop = reverse . dropWhile isPathSeparator . reverse
@@ -2024,11 +2027,16 @@ avroEncodeField :: AvroSchema.Field -> Val
2024
2027
avroEncodeField = \ case
2025
2028
AvroSchema. Field name aliases doc order typ def -> BoxedVal $ DataG Ty. avroFieldRef TT. avroFieldTag (segFromList [encodeVal (Util.Text. fromText name), encodeVal (map Util.Text. fromText aliases), encodeVal (Util.Text. fromText <$> doc), avroEncodeSchema typ, encodeVal (fmap avroEncodeOrder order), encodeVal (fmap avroEncodeDefaultValue def)])
2026
2029
2027
- avroDecodeBinary :: Closure -> Val -> IO (Either String Closure )
2028
- avroDecodeBinary readSchema bytes =
2029
- -- TODO: Handle lookup of named types. Currently we just assume
2030
- -- that the named types are already resolved.
2031
- error " TODO: avroDecodeBinary"
2030
+ avroDecodeBinary :: Closure -> Closure -> Bytes. Bytes -> IO Val
2031
+ avroDecodeBinary _env readSchema bytes = do
2032
+ -- envVal <- decodeVal @[(Closure, Closure)] (BoxedVal env)
2033
+ -- envDecoded <- traverse (bimapM avroDecodeTypeName avroDecodeReadSchema) envVal
2034
+ readSchemaDecoded <- avroDecodeReadSchema readSchema
2035
+ -- let envMap = (HashMap.fromList envDecoded) <> ReadSchema.extractBindings readSchemaDecoded
2036
+ -- TODO: Modify the avro library to allow us to call getField directly
2037
+ case Get. runGetOrFail (FromAvro. getValue readSchemaDecoded) (L. fromStrict (Bytes. toByteString bytes)) of
2038
+ Left (_, _, err) -> pure $ encodeVal @ (Either String Val ) (Left err)
2039
+ Right (_, _, value) -> pure $ encodeVal @ (Either String Val ) (Right (avroEncodeValue value))
2032
2040
2033
2041
-- go schema = case schema of
2034
2042
-- Enum _ t
@@ -2863,6 +2871,10 @@ functionReplacementList =
2863
2871
( " 01pl56v6v0n2labp71cp6darcbftlj7d4h9t718mkfpj6lc905ro4" ,
2864
2872
0 ,
2865
2873
Json_tryUnconsText
2874
+ ),
2875
+ ( " 01csmdujt5ot550j9t0o1gfop4ephtssv358rkfqdo2e01knekgds" ,
2876
+ 0 ,
2877
+ Avro_decodeBinary
2866
2878
)
2867
2879
]
2868
2880
0 commit comments