Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions share-api/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ library:
- condition: false
other-modules: Paths_share_api

tests:
local-reference-tests:
main: LocalReferencesSpec.hs
source-dirs: test
dependencies:
- share-api
- vector

executables:
share-api:
when:
Expand Down
162 changes: 161 additions & 1 deletion share-api/share-api.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.38.1.
-- This file has been generated from package.yaml by hpack version 0.39.6.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -77,6 +77,7 @@ library
Share.Postgres.Contributions.Ops
Share.Postgres.Contributions.Queries
Share.Postgres.Cursors
Share.Postgres.Definitions.LocalReferences
Share.Postgres.Definitions.Queries
Share.Postgres.Definitions.Types
Share.Postgres.Hashes.Queries
Expand Down Expand Up @@ -528,3 +529,162 @@ executable share-api
default-language: Haskell2010
if flag(queryspans)
cpp-options: -DQUERY_SPANS

test-suite local-reference-tests
type: exitcode-stdio-1.0
main-is: LocalReferencesSpec.hs
other-modules:
Paths_share_api
hs-source-dirs:
test
default-extensions:
ApplicativeDo
BangPatterns
BlockArguments
DeriveFunctor
DeriveGeneric
DeriveFoldable
DeriveTraversable
DerivingStrategies
DerivingVia
DuplicateRecordFields
DoAndIfThenElse
FlexibleContexts
FlexibleInstances
GeneralizedNewtypeDeriving
LambdaCase
MultiParamTypeClasses
NamedFieldPuns
OverloadedStrings
PatternSynonyms
RankNTypes
ScopedTypeVariables
TupleSections
TypeApplications
ViewPatterns
QuasiQuotes
BlockArguments
QuasiQuotes
ImportQualifiedPost
OverloadedRecordDot
NumericUnderscores
ghc-options: -Wall -Werror -Wname-shadowing -Wno-type-defaults -Wno-missing-pattern-synonym-signatures -Wincomplete-uni-patterns -Widentities -Wredundant-constraints -Wpartial-fields -fprint-expanded-synonyms -fwrite-ide-info -O2 -funbox-strict-fields
build-depends:
Diff
, MonadRandom
, aeson
, async
, base >=4.7 && <5
, base64-bytestring
, bifunctors
, binary
, binary-instances
, bytes
, bytestring
, case-insensitive
, cborg
, clock
, conduit
, containers
, cookie
, cryptonite
, data-default
, either
, extra
, fast-logger
, filepath
, free
, fuzzyfind
, generic-lens
, hasql
, hasql-interpolate
, hasql-listen-notify
, hasql-pool
, hedis
, here
, hs-opentelemetry-api
, hs-opentelemetry-instrumentation-wai
, hs-opentelemetry-sdk
, http-api-data
, http-client
, http-client-tls
, http-media
, http-types
, jose
, ki-unlifted
, lens
, megaparsec
, memory
, mmorph
, monad-validate
, monoidal-containers
, mtl
, network
, network-simple
, network-uri
, nonempty-containers
, parallel
, parser-combinators
, pem
, prometheus-client
, prometheus-metrics-ghc
, random
, raven-haskell
, safe
, semialign
, serialise
, servant
, servant-auth
, servant-client
, servant-client-core
, servant-conduit
, servant-server
, share-api
, share-auth
, share-utils
, stm
, stm-chans
, text
, these
, time
, tls
, transformers
, unison-codebase
, unison-codebase-sqlite
, unison-codebase-sqlite-hashing-v2
, unison-core
, unison-core1
, unison-hash
, unison-hashing-v2
, unison-merge
, unison-parser-typechecker
, unison-prelude
, unison-pretty-printer
, unison-runtime
, unison-share-api
, unison-share-projects-api
, unison-sqlite
, unison-syntax
, unison-util-base32hex
, unison-util-cache
, unison-util-recursion
, unison-util-relation
, unison-util-serialization
, unliftio
, unordered-containers
, uuid
, vault
, vector
, wai
, wai-cors
, wai-extra
, wai-middleware-prometheus
, warp
, websockets
, witch
, witherable
, x509
, x509-store
default-language: Haskell2010
if flag(queryspans)
cpp-options: -DQUERY_SPANS
71 changes: 71 additions & 0 deletions share-api/src/Share/Postgres/Definitions/LocalReferences.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Share.Postgres.Definitions.LocalReferences
( LocalReferenceError (..),
lookupLocalReference,
validateLocalReferenceMappings,
validateParallelLocalReferenceMappings,
)
where

import Data.Int (Int32)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Vector qualified as Vector

data LocalReferenceError
= MissingLocalReference Text Text Integer Int
| NonContiguousLocalReference Text Integer Integer
| MismatchedLocalReferenceMappings Text Int Int
deriving stock (Eq)

instance Show LocalReferenceError where
show = \case
MissingLocalReference context referenceKind localIndex vectorSize ->
"Missing local "
<> Text.unpack referenceKind
<> " reference at index "
<> show localIndex
<> " while resolving "
<> Text.unpack context
<> " (lookup vector size "
<> show vectorSize
<> ")"
NonContiguousLocalReference context expectedIndex actualIndex ->
"Non-contiguous local reference indices for "
<> Text.unpack context
<> ": expected "
<> show expectedIndex
<> ", got "
<> show actualIndex
MismatchedLocalReferenceMappings context indices values ->
"Mismatched local reference index/value counts for "
<> Text.unpack context
<> ": "
<> show indices
<> " indices, "
<> show values
<> " values"

lookupLocalReference :: (Integral i) => Text -> Text -> i -> Vector.Vector a -> Either LocalReferenceError a
lookupLocalReference context referenceKind localIndex values =
if indexInteger < 0 || indexInteger > toInteger (maxBound :: Int)
then missing
else case (Vector.!?) values (fromInteger indexInteger) of
Just value -> Right value
Nothing -> missing
where
indexInteger = toInteger localIndex
missing = Left $ MissingLocalReference context referenceKind indexInteger (Vector.length values)

validateLocalReferenceMappings :: Text -> [(Int32, a)] -> Either LocalReferenceError [a]
validateLocalReferenceMappings context = go 0
where
go _ [] = Right []
go expected ((actual, value) : rest)
| toInteger actual == expected = (value :) <$> go (expected + 1) rest
| otherwise = Left $ NonContiguousLocalReference context expected (toInteger actual)

validateParallelLocalReferenceMappings :: Text -> [Int32] -> [a] -> Either LocalReferenceError [a]
validateParallelLocalReferenceMappings context indices values
| length indices /= length values =
Left $ MismatchedLocalReferenceMappings context (length indices) (length values)
| otherwise = validateLocalReferenceMappings context (zip indices values)
Loading