From 7a1b27e8fcacb184258e35f68ed96eedab021f9a Mon Sep 17 00:00:00 2001 From: Josef Svenningsson Date: Thu, 21 Nov 2024 01:55:07 -0800 Subject: [PATCH] Use lists to encode sets in Thrift Summary: The set type in Hack is very limited and cannot have e.g. predicates as elements. Since the thrift set type translates to the Hack set type, that means that we cannot use sets in thrift to encode sets in Angle. This is really sad because it means that we cannot use sets in other language APIs, like Haskell :-( Reviewed By: donsbot Differential Revision: D66158787 fbshipit-source-id: 05beed53d0e5de40c185848b6eae16c62024354c --- glean/db/Glean/Query/JSON.hs | 3 ++- glean/schema/gen/Glean/Schema/Gen/Haskell.hs | 3 +-- glean/schema/gen/Glean/Schema/Gen/Thrift.hs | 2 +- glean/test/lib/TestData.hs | 8 +++----- glean/test/tests/Angle/AngleTest.hs | 10 +++++----- glean/test/tests/EncodingTest.hs | 7 +++---- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/glean/db/Glean/Query/JSON.hs b/glean/db/Glean/Query/JSON.hs index c00a8b84a..bca464189 100644 --- a/glean/db/Glean/Query/JSON.hs +++ b/glean/db/Glean/Query/JSON.hs @@ -355,7 +355,8 @@ thriftType NatTy{} = 6 thriftType StringTy{} = 8 thriftType (ArrayTy ByteTy{}) = 8 thriftType ArrayTy{} = 9 -thriftType SetTy{} = 10 +thriftType (SetTy ByteTy{}) = 8 +thriftType SetTy{} = 9 thriftType RecordTy{} = 12 thriftType SumTy{} = 12 thriftType (NamedTy (ExpandedType _ ty)) = thriftType ty diff --git a/glean/schema/gen/Glean/Schema/Gen/Haskell.hs b/glean/schema/gen/Glean/Schema/Gen/Haskell.hs index 9b989e925..403ea754f 100644 --- a/glean/schema/gen/Glean/Schema/Gen/Haskell.hs +++ b/glean/schema/gen/Glean/Schema/Gen/Haskell.hs @@ -119,7 +119,6 @@ header here deps = Text.unlines $ , "import qualified Data.ByteString" , "import qualified Data.Default" , "import qualified Data.Text" - , "import qualified Data.Set" , "" -- we should use qualified imports as far as possible to avoid -- clashing with Thrift-generated code @@ -246,7 +245,7 @@ haskellTy_ withId genSub here t = case t of SumTy{} -> shareTypeDef genSub here t SetTy tInner -> do inner <- haskellTy_ PredName genSub here tInner - return $ "Data.Set.Set " <> inner + return $ "[" <> inner <> "]" MaybeTy ty -> do inner <- haskellTy_ PredName genSub here ty return (optionalize inner) diff --git a/glean/schema/gen/Glean/Schema/Gen/Thrift.hs b/glean/schema/gen/Glean/Schema/Gen/Thrift.hs index 2643e3f31..28316c1a5 100644 --- a/glean/schema/gen/Glean/Schema/Gen/Thrift.hs +++ b/glean/schema/gen/Glean/Schema/Gen/Thrift.hs @@ -321,7 +321,7 @@ thriftTy here t = case t of SumTy{} -> shareTypeDef here t SetTy ty -> do inner <- thriftTy here ty - return $ "set<" <> inner <> ">" + return $ "list<" <> inner <> ">" MaybeTy tInner -> do inner <- thriftTy here tInner return (optionalize inner) diff --git a/glean/test/lib/TestData.hs b/glean/test/lib/TestData.hs index 0003e6f1e..1b1e9c17e 100644 --- a/glean/test/lib/TestData.hs +++ b/glean/test/lib/TestData.hs @@ -15,7 +15,6 @@ module TestData ) where import Data.Default -import qualified Data.Set as Set import Glean.Typed import Glean.Types @@ -43,9 +42,8 @@ kitchenSink1 = def , Glean.Test.kitchenSink_maybe_ = Just def , Glean.Test.kitchenSink_bool_ = True , Glean.Test.kitchenSink_string_ = "Hello\0world!\0" - , Glean.Test.kitchenSink_set_of_nat = - Set.fromList [toNat 1, toNat 2, toNat 65535] - , Glean.Test.kitchenSink_set_of_string = Set.fromList ["apa", "bepa"] + , Glean.Test.kitchenSink_set_of_nat = [toNat 65535, toNat 1, toNat 2 ] + , Glean.Test.kitchenSink_set_of_string = ["apa", "bepa"] } mkTestFacts :: NewFact m => (m () -> m ()) -> (m () -> m ()) -> m () @@ -108,7 +106,7 @@ mkTestFacts first second = do , Glean.Test.kitchenSink_named_sum_ = Glean.Test.Sum_wed True , Glean.Test.kitchenSink_named_record_ = rec , Glean.Test.kitchenSink_maybe_ = Nothing - , Glean.Test.kitchenSink_set_of_pred = Set.fromList [kitchenSink2Fact0] + , Glean.Test.kitchenSink_set_of_pred = [kitchenSink2Fact0] } kitchenSink2Term1b = kitchenSink2Term1 diff --git a/glean/test/tests/Angle/AngleTest.hs b/glean/test/tests/Angle/AngleTest.hs index 28aee2e73..fe4699ea9 100644 --- a/glean/test/tests/Angle/AngleTest.hs +++ b/glean/test/tests/Angle/AngleTest.hs @@ -85,11 +85,11 @@ angleTest modify = dbTestCase $ \env repo -> do glean.test.Predicate { named_sum_ = { tue = 37 } } |] print results - assertBool "angle - glean.test.Predicate 1" $ - case results of - [Glean.Test.Predicate{Glean.Test.predicate_key = Just k}] -> - ignorePredK k == ignorePredK kitchenSink1 - _ -> False + case results of + [Glean.Test.Predicate{Glean.Test.predicate_key = Just k}] -> + assertEqual "angle - glean.test.Predicate 1" + (ignorePredK kitchenSink1) (ignorePredK k) + _ -> assertBool "angle - glean.test.Predicate 1" False -- match all results (two) results <- runQuery_ env repo $ modify $ angle @Glean.Test.Predicate diff --git a/glean/test/tests/EncodingTest.hs b/glean/test/tests/EncodingTest.hs index 90e758c3c..93253cd4a 100644 --- a/glean/test/tests/EncodingTest.hs +++ b/glean/test/tests/EncodingTest.hs @@ -14,7 +14,6 @@ import qualified Data.ByteString.Base16 as Hex import qualified Data.ByteString.Char8 as ByteString import Data.Default import Data.IntMap.Strict (IntMap) -import qualified Data.Set as Set import Foreign.C.String import Foreign.C.Types import Foreign.Ptr @@ -90,11 +89,11 @@ kitchenSink = KitchenSink , kitchenSink_array2_of_string = [ [ "a", "bc", "def" ] ] , kitchenSink_set_of_nat = - Set.fromList [ Nat 1, Nat 3 ] + [ Nat 1, Nat 3 ] , kitchenSink_set_of_string = - Set.fromList [ "foo", "bar" ] + [ "foo", "bar" ] , kitchenSink_set_of_pred = - Set.fromList [ Glean.Test.Predicate 5432 Nothing ] + [ Glean.Test.Predicate 5432 Nothing ] } data E = E