Skip to content

Commit

Permalink
Fix some formatting errors, improve comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljungberg committed Nov 29, 2022
1 parent 46298c5 commit 434a7b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions src/PostgREST/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ resolveTableField :: Table -> Field -> CoercibleField
resolveTableField table (fieldName, []) = resolveTableFieldName table fieldName
-- If the field is known and a JSON path is given, always assume the JSON type. But don't assume a type for entirely unknown fields.
resolveTableField table (fieldName, jp) =
case resolveTableFieldName table fieldName of
tf@CoercibleField{tfIRType=""} -> tf{tfJsonPath=jp}
tf -> tf{tfJsonPath=jp, tfIRType="json"}
case resolveTableFieldName table fieldName of
tf@CoercibleField{tfIRType=""} -> tf{tfJsonPath=jp}
tf -> tf{tfJsonPath=jp, tfIRType="json"}

-- | Resolve a type within the context based on the given field name and JSON path. Although there are situations where failure to resolve a field is considered an error (see `resolveOrError`), there are also situations where we allow it (RPC calls). If it should be an error and `resolveOrError` doesn't fit, ensure to check the `tfIRType` isn't empty.
resolveTypeOrUnknown :: ResolverContext -> Field -> CoercibleField
Expand Down Expand Up @@ -172,12 +172,12 @@ readPlan qi@QualifiedIdentifier{..} AppConfig{configDbMaxRows} SchemaCache{dbTab
validateSpreadEmbeds =<<
addRelatedOrders =<<
addRels qiSchema (iAction apiRequest) dbRelationships Nothing =<<
addLogicTrees ctx apiRequest =<<
addLogicTrees ctx apiRequest =<<
addRanges apiRequest =<<
addOrders apiRequest =<<
addDataRepresentationAliases =<<
expandStarsForDataRepresentations ctx =<<
addFilters ctx apiRequest (initReadRequest ctx $ QueryParams.qsSelect $ iQueryParams apiRequest)
expandStarsForDataRepresentations ctx =<<
addFilters ctx apiRequest (initReadRequest ctx $ QueryParams.qsSelect $ iQueryParams apiRequest)

-- Build the initial read plan tree
initReadRequest :: ResolverContext -> [Tree SelectItem] -> ReadPlanTree
Expand Down Expand Up @@ -525,12 +525,12 @@ mutatePlan mutation qi ApiRequest{..} SchemaCache{dbTables, dbRepresentations} r
if iPreferRepresentation == None
then []
else inferColsEmbedNeeds readReq pkCols
pkCols = maybe mempty tablePKCols $ HM.lookup qi dbTables
tbl = HM.lookup qi dbTables
pkCols = maybe mempty tablePKCols tbl
logic = map (resolveLogicTree ctx . snd) qsLogic
rootOrder = maybe [] snd $ find (\(x, _) -> null x) qsOrder
combinedLogic = foldr (addFilterToLogicForest . resolveFilter ctx) logic qsFiltersRoot
body = payRaw <$> iPayload -- the body is assumed to be json at this stage(ApiRequest validates)
tbl = HM.lookup qi dbTables
typedColumnsOrError = resolveOrError ctx tbl `traverse` S.toList iColumns

resolveOrError :: ResolverContext -> Maybe Table -> FieldName -> Either ApiRequestError CoercibleField
Expand Down
16 changes: 8 additions & 8 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ removeInternal schemas dbStruct =
, dbRelationships = filter (\r -> qiSchema (relForeignTable r) `elem` schemas && not (hasInternalJunction r)) <$>
HM.filterWithKey (\(QualifiedIdentifier sch _, _) _ -> sch `elem` schemas ) (dbRelationships dbStruct)
, dbProcs = dbProcs dbStruct -- procs are only obtained from the exposed schemas, no need to filter them.
, dbRepresentations = dbRepresentations dbStruct -- it's expected to use internal schemas for casting functions; we don't expose these functions through the API, we only use them
, dbRepresentations = dbRepresentations dbStruct -- no need to filter, not directly exposed through the API
}
where
hasInternalJunction ComputedRelationship{} = False
Expand Down Expand Up @@ -296,21 +296,21 @@ dataRepresentations = SQL.Statement sql (arrayParam HE.text) decodeRepresentatio
where
sql = [q|
SELECT
c.castsource::regtype::text,
c.casttarget::regtype::text,
c.castfunc::regproc::text
c.castsource::regtype::text,
c.casttarget::regtype::text,
c.castfunc::regproc::text
FROM
pg_catalog.pg_cast c
JOIN pg_catalog.pg_type src_t
ON c.castsource::oid = src_t.oid
JOIN pg_catalog.pg_type dst_t
ON c.casttarget::oid = dst_t.oid
ON c.castsource::oid = src_t.oid
JOIN pg_catalog.pg_type dst_t
ON c.casttarget::oid = dst_t.oid
WHERE
c.castcontext = 'i'
AND c.castmethod = 'f'
AND has_function_privilege(c.castfunc, 'execute')
AND ((src_t.typtype = 'd' AND c.casttarget IN ('json'::regtype::oid , 'text'::regtype::oid))
OR (dst_t.typtype = 'd' AND c.castsource IN ('json'::regtype::oid , 'text'::regtype::oid)))
OR (dst_t.typtype = 'd' AND c.castsource IN ('json'::regtype::oid , 'text'::regtype::oid)))
|]

allProcs :: PgVersion -> Bool -> SQL.Statement [Schema] ProcsMap
Expand Down
8 changes: 4 additions & 4 deletions src/PostgREST/SchemaCache/Representations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import qualified Data.HashMap.Strict as HM

import Protolude

-- Supporting information for how to receive and return data in API endpoints. This structure answers questions like:
-- | Data representations allow user customisation of how to present and receive data through APIs, per field.
-- This structure is used for the library of available transforms. It answers questions like:
-- - What function, if any, should be used to present a certain field that's been selected for API output?
-- - How do we parse incoming data for a certain field type when inserting or updating?
-- - And similarly, how do we parse textual data in a query string to be used as a filter?
-- In the future it's likely this functionality will be extended to cover the outputs and inputs of procedures for RPC.
--
-- Support for outputting special formats like CSV and binary data would fit into the same system.
data DataRepresentation = DataRepresentation
{ drSourceType :: Text
, drTargetType :: Text
, drFunction :: Text
} deriving (Eq, Show, Generic, JSON.ToJSON, JSON.FromJSON)

-- The representation map maps from (source type, target type) to a DR. The absence of a pair here means use the default
-- PostgreSQL type conversion.
-- The representation map maps from (source type, target type) to a DR.
type RepresentationsMap = HM.HashMap (Text, Text) DataRepresentation

0 comments on commit 434a7b9

Please sign in to comment.