Skip to content

Commit

Permalink
Eliminated resolveField. Other clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljungberg committed Nov 25, 2022
1 parent 364e83c commit 2212619
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
13 changes: 4 additions & 9 deletions src/PostgREST/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ data ResolverContext = ResolverContext
, outputType :: Text -- ^ The output type for the response payload; e.g. "csv", "json", "binary".
}

resolveField :: Field -> Text -> CoercibleField
resolveField (fieldName, jsonPath) irType = CoercibleField fieldName jsonPath irType Nothing

resolveColumnField :: Column -> CoercibleField
resolveColumnField col = resolveField (colName col, []) (colNominalType col)
resolveColumnField col = CoercibleField (colName col) [] (colNominalType col) Nothing

resolveTableFieldName :: Table -> FieldName -> CoercibleField
resolveTableFieldName table fieldName =
Expand All @@ -125,9 +122,7 @@ resolveTableField table (fieldName, jp) =
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). This function is specifically for when it's not an error to fail resolving a field.
-- | 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
resolveTypeOrUnknown ResolverContext{..} field@(fn, jp) =
fromMaybe (unknownField fn jp) $ HM.lookup qi tables >>=
Expand Down Expand Up @@ -165,7 +160,7 @@ resolveQueryInputField ctx field = withTextParse ctx $ resolveTypeOrUnknown ctx
readPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> Either Error ReadPlanTree
readPlan qi@QualifiedIdentifier{..} AppConfig{configDbMaxRows} SchemaCache{dbTables, dbRelationships, dbRepresentations} apiRequest =
let
-- | JSON output format hardcoded for now. In the future we might want to support other output mappings such as CSV.
-- JSON output format hardcoded for now. In the future we might want to support other output mappings such as CSV.
ctx = ResolverContext dbTables dbRepresentations qi "json"
in
mapLeft ApiRequestError $
Expand Down Expand Up @@ -541,7 +536,7 @@ resolveOrError _ Nothing _ = Left NotFound
resolveOrError ctx (Just table) field =
case resolveTableFieldName table field of
CoercibleField{tfIRType=""} -> Left $ ColumnNotFound field
cf -> Right $ withJsonParse ctx cf
cf -> Right $ withJsonParse ctx cf

callPlan :: ProcDescription -> ApiRequest -> ReadPlanTree -> CallPlan
callPlan proc apiReq readReq = FunctionCall {
Expand Down
3 changes: 2 additions & 1 deletion src/PostgREST/Plan/MutatePlan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import qualified Data.ByteString.Lazy as LBS

import PostgREST.ApiRequest.Preferences (PreferResolution)
import PostgREST.ApiRequest.Types (OrderTerm)
import PostgREST.Plan.Types (CoercibleField, TypedLogicTree)
import PostgREST.Plan.Types (CoercibleField,
TypedLogicTree)
import PostgREST.RangeQuery (NonnegRange)
import PostgREST.SchemaCache.Identifiers (FieldName,
QualifiedIdentifier)
Expand Down
5 changes: 2 additions & 3 deletions src/PostgREST/Plan/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ module PostgREST.Plan.Types
, TransformerProc
) where

import PostgREST.ApiRequest.Types (JsonPath, LogicOperator,
OpExpr)
import PostgREST.ApiRequest.Types (JsonPath, LogicOperator, OpExpr)

import PostgREST.SchemaCache.Identifiers (FieldName)

import Protolude

type TransformerProc = Text

-- | A CoercibleField pairs the name of a query element with any type coercion information we need for that particular use.
-- | A CoercibleField pairs the name of a query element with any type coercion information we need for some specific use case.
-- |
-- | As suggested by the name, it's often a reference to a field in a table but really it can be any nameable element (function parameter, calculation with an alias, etc) with a knowable type.
-- |
Expand Down
6 changes: 3 additions & 3 deletions src/PostgREST/Query/SqlFragment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ import PostgREST.MediaType (MTPlanFormat (..),
MTPlanOption (..))
import PostgREST.Plan.ReadPlan (JoinCondition (..))
import PostgREST.Plan.Types (CoercibleField (..),
unknownField,
TypedFilter (..),
TypedLogicTree (..))
TypedLogicTree (..),
unknownField)
import PostgREST.RangeQuery (NonnegRange, allRange,
rangeLimit, rangeOffset)
import PostgREST.SchemaCache.Identifiers (FieldName,
Expand Down Expand Up @@ -297,7 +297,7 @@ pgFmtUnknownLiteralForField value _ = value

-- | Array version of the above, used by ANY().
pgFmtArrayLiteralForField :: [Text] -> CoercibleField -> SQL.Snippet
pgFmtArrayLiteralForField values CoercibleField{tfTransform=(Just parserProc)} = SQL.sql "ARRAY[" <> (intercalateSnippet ", " $ pgFmtCallUnary parserProc . unknownLiteral <$> values) <> "]"
pgFmtArrayLiteralForField values CoercibleField{tfTransform=(Just parserProc)} = SQL.sql "ARRAY[" <> intercalateSnippet ", " (pgFmtCallUnary parserProc . unknownLiteral <$> values) <> "]"
-- When no transformation is requested, use an array literal which should be simpler, maybe faster.
pgFmtArrayLiteralForField values _ = unknownLiteral (pgBuildArrayLiteral values)

Expand Down

0 comments on commit 2212619

Please sign in to comment.