Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode Long and ULong values as strings in json #122

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/Ingest/GaiaDR3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ indexLevel :: Int
indexLevel = 59 - 2*level where level = 8

-- |convert from source_id to file naming, HEALpix index level 8
keyIndex :: Key -> Int
keyIndex = fromIntegral . (`shiftR` indexLevel)
_keyIndex :: Key -> Int
_keyIndex = fromIntegral . (`shiftR` indexLevel)

indexKey :: Int -> Key
indexKey = (`shiftL` indexLevel) . fromIntegral
Expand Down
3 changes: 1 addition & 2 deletions src/Ingest/HDF5.hs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@
gdir = dir </> "groups"
isdm = "-Dark" `T.isSuffixOf` simn
snap3 = printf "%03d" snap
next Ingest{ ingestStart = s, ingestSize = Just z, ingestOffset = o, ingestJoin = j } = inginfo

Check warning on line 444 in src/Ingest/HDF5.hs

View workflow job for this annotation

GitHub Actions / stack build

Pattern match(es) are non-exhaustive

Check warning on line 444 in src/Ingest/HDF5.hs

View workflow job for this annotation

GitHub Actions / stack build

Pattern match(es) are non-exhaustive
{ ingestStart = s + z
, ingestSize = Nothing
, ingestOffset = if o > z then o - z else 0
Expand Down Expand Up @@ -622,9 +622,8 @@
return (n, b)
nsub <- case ingestJoin info of
Just IngestHaloJoin
{ joinIngest = subinfo@Ingest{ ingestCatalog = Catalog{ catalogFields = subcat }, ingestJoin = Just (IngestJoin supfs) }
{ joinIngest = subinfo@Ingest{ ingestJoin = Just (IngestJoin supfs) }
, joinFirst = (`lookup` fof) -> Just (Long fofid)
, joinCount = fofcountf
, joinParent = subgf
} -> liftBaseOp (withGroup hf (simn <> "_Subhalo")) $ \hs -> do
let fofmap = IM.fromDistinctAscList $ zip (map fromIntegral $ VS.toList fofid) [0..]
Expand All @@ -637,7 +636,7 @@
{ ingestCatalog = (ingestCatalog subinfo){ catalogFieldGroups = fold (fieldSub f) }
, ingestOffset = 0
}) hg sups
loop info sups = do

Check warning on line 639 in src/Ingest/HDF5.hs

View workflow job for this annotation

GitHub Actions / stack build

This binding for ‘info’ shadows the existing binding

Check warning on line 639 in src/Ingest/HDF5.hs

View workflow job for this annotation

GitHub Actions / stack build

This binding for ‘info’ shadows the existing binding
liftIO $ rePutStr ("subhalo " <> show (ingestOffset info)) >> hFlush stdout
(n, sb, info') <- liftIO $ loadBlock info hs
gid <- case lookup subgf sb of
Expand Down
20 changes: 18 additions & 2 deletions src/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module Type

import Control.Applicative ((<|>), many, empty)
import qualified Data.Aeson as J
import qualified Data.Aeson.Encoding as JE
import qualified Data.Aeson.Types as J
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as B
Expand Down Expand Up @@ -305,9 +306,24 @@ instance {-# OVERLAPPABLE #-} Show1 f => Show (TypeValue f) where
instance Show Value where
showsPrec i = unTypeValue (showsPrec i . runIdentity)

_maxSafeJsInt :: Int64
_maxSafeJsInt = 9007199254740991

isUnsafeJs :: TypeValue f -> Bool
isUnsafeJs (Long _) = True
isUnsafeJs (ULong _) = True
isUnsafeJs _ = False

instance {-# OVERLAPPABLE #-} J.ToJSON1 f => J.ToJSON (TypeValue f) where
toJSON = unTypeValue J.toJSON1
toEncoding = unTypeValue J.toEncoding1
toJSON v
| isUnsafeJs v = unTypeValue (J.liftToJSON (J.toJSON . show) (J.toJSONList . map show)) v
| otherwise = unTypeValue J.toJSON1 v
toEncoding v
| isUnsafeJs v = unTypeValue (J.liftToEncoding toEncodingString (JE.list toEncodingString)) v
| otherwise = unTypeValue J.toEncoding1 v
where
toEncodingString :: Typed a => a -> J.Encoding
toEncodingString x = J.unsafeToEncoding $ B.char7 '"' <> renderValue x <> B.char7 '"'

parseJSONOrStringValue :: Typed a => J.Value -> J.Parser a
parseJSONOrStringValue j@(J.String s) = parseJSONValue j <|> maybe (fail "parseStringValue") return (readValue $ TE.encodeUtf8 s)
Expand Down
Loading