server: report duplicate entries in metadata lists using ToTxt instead of Show

This change is because in https://github.com/hasura/graphql-engine-mono/pull/2477, errors come out as
```
multiple declarations exist for the following allowlist : [CollectionName {unCollectionName = NonEmptyText {unNonEmptyText = "collection_1"}}]'
```
which is awfully messy. This changes it to
```
multiple declarations exist for the following allowlist: collection_1
```

This should improve error messages across the board -- e.g. `RoleName` has a nice `ToTxt` instance, while we used to use a derived `Show` instances. However, there might just be instances where the `Show` instance was better, bit hard to be sure based on scanning the code since we don't have test coverage for these errors.

Broken out of the allowlist PR since it affects more than just the allowlist.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3470
GitOrigin-RevId: 7a2c29f17d9f15d840cb2f89caefcdd3aae44d25
This commit is contained in:
Robert 2022-02-01 12:29:53 +01:00 committed by hasura-bot
parent 670e0953fb
commit 0c17f238a2

View File

@ -62,7 +62,6 @@ module Hasura.RQL.Types.Metadata
mkSourceMetadata,
mkTableMeta,
noMetadataModify,
parseListAsMap,
parseNonSourcesMetadata,
rsmComment,
rsmDefinition,
@ -138,21 +137,22 @@ import Hasura.SQL.Tag
import Hasura.Session
import Language.GraphQL.Draft.Syntax qualified as G
-- | Raise exception if parsed list has multiple declarations
-- | Parse a list of objects into a map from a derived key,
-- failing if the list has duplicates.
parseListAsMap ::
(Hashable k, Eq k, Show k) =>
(Hashable k, Eq k, T.ToTxt k) =>
Text ->
(a -> k) ->
Parser [a] ->
Parser (InsOrdHashMap k a)
parseListAsMap t mapFn listP = do
parseListAsMap things mapFn listP = do
list <- listP
let duplicates = toList $ L.duplicates $ map mapFn list
unless (null duplicates) $
fail $
T.unpack $
"multiple declarations exist for the following " <> t <> " : "
<> tshow duplicates
"multiple declarations exist for the following " <> things <> ": "
<> T.commaSeparated duplicates
pure $ oMapFromL mapFn list
-- | Versioning the @'Metadata' JSON structure to track backwards incompatible changes.