From d03d68c25f5f917b1736b5702a3abea288d54196 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 22 Nov 2015 15:00:28 -0800 Subject: [PATCH] Simplify checking object keys for equality --- src/PostgREST/RequestIntent.hs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/PostgREST/RequestIntent.hs b/src/PostgREST/RequestIntent.hs index f802eac26..b21f45870 100644 --- a/src/PostgREST/RequestIntent.hs +++ b/src/PostgREST/RequestIntent.hs @@ -201,19 +201,14 @@ ensureUniform :: JSON.Array -> Maybe UniformObjects ensureUniform arr = let objs :: V.Vector JSON.Object objs = foldr -- filter non-objects, map to raw objects - (\result val -> case val of + (\val result -> case val of JSON.Object o -> V.cons o result _ -> result) V.empty arr - keysPerObj :: [S.Set T.Text] - keysPerObj = V.toList $ V.map (S.fromList . M.keys) objs - allKeys :: S.Set T.Text - allKeys = S.unions keysPerObj - commonKeys :: S.Set T.Text - commonKeys = case keysPerObj of - h : _ -> foldr S.intersection h keysPerObj - [] -> S.empty in + keysPerObj = V.map (S.fromList . M.keys) objs + canonicalKeys = fromMaybe S.empty $ keysPerObj V.!? 0 + areKeysUniform = all (==canonicalKeys) keysPerObj in - if (length objs == length arr) && (allKeys == commonKeys) + if (V.length objs == V.length arr) && areKeysUniform then Just (UniformObjects objs) else Nothing