Skip to content

Commit 6410a90

Browse files
committed
wip
1 parent 22ec1b3 commit 6410a90

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

unison-runtime/src/Unison/Runtime/Foreign/Function.hs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Data.Avro qualified as Avro
3636
import Data.Avro.Encoding.FromAvro qualified as FromAvro
3737
import Data.Avro.Schema.ReadSchema qualified as ReadSchema
3838
import Data.Avro.Schema.Schema qualified as AvroSchema
39+
import Data.Binary.Get qualified as Get
3940
import Data.Bitraversable (bimapM)
4041
import Data.Bits (shiftL, shiftR, (.|.))
4142
import Data.ByteArray qualified as BA
@@ -948,6 +949,8 @@ foreignCallHelper = \case
948949
errv = encodeJsonParseError err
949950
Json_tryUnconsText -> mkForeign $ \(txt :: Text) ->
950951
pure . bimap encodeJsonParseError (second encodeVal) $ parseJson txt
952+
Avro_decodeBinary -> mkForeign $ \(env :: Closure, readSchema :: Closure, bytes :: Bytes.Bytes) -> do
953+
avroDecodeBinary env readSchema bytes
951954
where
952955
forceListSpine xs = foldl (\u x -> x `seq` u) xs xs
953956
chop = reverse . dropWhile isPathSeparator . reverse
@@ -2024,11 +2027,16 @@ avroEncodeField :: AvroSchema.Field -> Val
20242027
avroEncodeField = \case
20252028
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)])
20262029

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))
20322040

20332041
-- go schema = case schema of
20342042
-- Enum _ t
@@ -2863,6 +2871,10 @@ functionReplacementList =
28632871
( "01pl56v6v0n2labp71cp6darcbftlj7d4h9t718mkfpj6lc905ro4",
28642872
0,
28652873
Json_tryUnconsText
2874+
),
2875+
( "01csmdujt5ot550j9t0o1gfop4ephtssv358rkfqdo2e01knekgds",
2876+
0,
2877+
Avro_decodeBinary
28662878
)
28672879
]
28682880

unison-runtime/src/Unison/Runtime/Foreign/Function/Type.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ data ForeignFunc
277277
| Json_toText
278278
| Json_unconsText
279279
| Json_tryUnconsText
280+
| Avro_decodeBinary
280281
deriving (Show, Eq, Ord, Enum, Bounded)
281282

282283
foreignFuncBuiltinName :: ForeignFunc -> Text
@@ -550,3 +551,4 @@ foreignFuncBuiltinName = \case
550551
Json_toText -> "Json.toText"
551552
Json_unconsText -> "Json.unconsText"
552553
Json_tryUnconsText -> "Json.tryUnconsText"
554+
Avro_decodeBinary -> "Avro.decodeBinary"

0 commit comments

Comments
 (0)