From 0c17f238a2f431d301111056a9ab6a76507d9489 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 1 Feb 2022 12:29:53 +0100 Subject: [PATCH] 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 --- server/src-lib/Hasura/RQL/Types/Metadata.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/src-lib/Hasura/RQL/Types/Metadata.hs b/server/src-lib/Hasura/RQL/Types/Metadata.hs index fcdcfd3ebf6..96793649fda 100644 --- a/server/src-lib/Hasura/RQL/Types/Metadata.hs +++ b/server/src-lib/Hasura/RQL/Types/Metadata.hs @@ -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.