|
| 1 | +{- |
| 2 | + Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + All rights reserved. |
| 4 | +
|
| 5 | + This source code is licensed under the BSD-style license found in the |
| 6 | + LICENSE file in the root directory of this source tree. |
| 7 | +-} |
| 8 | + |
| 9 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 10 | + |
| 11 | +module Glean.Glass.SymbolId.Angle ( |
| 12 | + {- instances and -} |
| 13 | + ) where |
| 14 | + |
| 15 | +import qualified Glean |
| 16 | + |
| 17 | +import Glean.Glass.SymbolId.Class |
| 18 | +import Control.Monad.Catch ( throwM ) |
| 19 | +import Glean.Glass.Types (Path(..), Name(..)) |
| 20 | +import qualified Glean.Haxl.Repos as Glean |
| 21 | +import qualified Glean.Schema.Anglelang.Types as A |
| 22 | +import qualified Glean.Schema.CodeAnglelang.Types as A |
| 23 | +import Data.Text (Text) |
| 24 | +import qualified Data.Text as Text |
| 25 | + |
| 26 | +instance Symbol A.Entity where |
| 27 | + toSymbol _ = throwM $ SymbolError "Angle.Entity: use toSymbolWithPath" |
| 28 | + toSymbolWithPath e (Path p) = do |
| 29 | + syms <- toSymbol $ A.entity_decl e |
| 30 | + return $ case syms of |
| 31 | + [] -> [] |
| 32 | + (x:xs) -> Text.splitOn "/" p <> (x:xs) |
| 33 | + |
| 34 | +instance Symbol A.Declaration where |
| 35 | + toSymbol d = case d of |
| 36 | + A.Declaration_pred x -> toSymbolPredicate x |
| 37 | + A.Declaration_ty x -> toSymbolPredicate x |
| 38 | + A.Declaration_schema x -> toSymbolPredicate x |
| 39 | + A.Declaration_imp x -> toSymbol x |
| 40 | + A.Declaration_derive_ x -> toSymbolPredicate x |
| 41 | + A.Declaration_evolve _ -> return [] |
| 42 | + A.Declaration_EMPTY -> return [] |
| 43 | + |
| 44 | +instance Symbol A.PredicateDecl_key where |
| 45 | + toSymbol A.PredicateDecl_key{..} = toSymbol predicateDecl_key_name |
| 46 | + |
| 47 | +instance Symbol A.TypeDecl_key where |
| 48 | + toSymbol A.TypeDecl_key{..} = toSymbol typeDecl_key_name |
| 49 | + |
| 50 | +instance Symbol A.SchemaDecl_key where |
| 51 | + toSymbol A.SchemaDecl_key{..} = toSymbol schemaDecl_key_name |
| 52 | + |
| 53 | +instance Symbol A.DerivingDecl_key where |
| 54 | + toSymbol (A.DerivingDecl_key n d) = do |
| 55 | + n' <- toSymbol n |
| 56 | + return $ n' ++ [Text.pack $ "_" ++ show d] |
| 57 | + |
| 58 | +instance Symbol A.Name where |
| 59 | + toSymbol k = do |
| 60 | + v <- Glean.keyOf k |
| 61 | + return [v] |
| 62 | + |
| 63 | + |
| 64 | +-- Searching for Angle Entities |
| 65 | +instance ToQName A.Entity where |
| 66 | + toQName e = toQName $ A.entity_decl e |
| 67 | + |
| 68 | +instance ToQName A.Declaration where |
| 69 | + toQName d = case d of |
| 70 | + A.Declaration_pred x -> Glean.keyOf x >>= toQName |
| 71 | + A.Declaration_ty x -> Glean.keyOf x >>= toQName |
| 72 | + A.Declaration_schema x -> Glean.keyOf x >>= toQName |
| 73 | + A.Declaration_imp x -> toQNameEmptyNs x |
| 74 | + A.Declaration_evolve _ -> return $ Left "evolve toQName: not supported" |
| 75 | + A.Declaration_derive_ _ -> return $ Left "derive toQName: not supported" |
| 76 | + A.Declaration_EMPTY -> return $ Left "unknown Declaration" |
| 77 | + |
| 78 | +instance ToQName A.PredicateDecl_key where |
| 79 | + toQName A.PredicateDecl_key{..} = toQNameEmptyNs predicateDecl_key_name |
| 80 | + |
| 81 | +instance ToQName A.TypeDecl_key where |
| 82 | + toQName A.TypeDecl_key{..} = toQNameEmptyNs typeDecl_key_name |
| 83 | + |
| 84 | +instance ToQName A.SchemaDecl_key where |
| 85 | + toQName A.SchemaDecl_key{..} = toQNameEmptyNs schemaDecl_key_name |
| 86 | + |
| 87 | +toQNameEmptyNs :: A.Name -> Glean.RepoHaxl u w (Either Text (Name, Name)) |
| 88 | +toQNameEmptyNs name = do |
| 89 | + nameStr <- Glean.keyOf name |
| 90 | + return $ Right (Name nameStr, Name mempty) |
0 commit comments