Skip to content

Commit be0d7ff

Browse files
authored
Port from codec-argonaut to codec-json (#690)
1 parent c36b378 commit be0d7ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+776
-665
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ result
1717

1818
# Keep it secret, keep it safe.
1919
.env
20+
.envrc

app/spago.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package:
88
dependencies:
99
- aff
1010
- ansi
11-
- argonaut-core
1211
- arrays
1312
- b64
1413
- bifunctors
15-
- codec-argonaut
14+
- codec
15+
- codec-json
1616
- console
1717
- const
1818
- control
@@ -23,22 +23,23 @@ package:
2323
- either
2424
- exceptions
2525
- exists
26-
- filterable
2726
- fetch
27+
- filterable
2828
- foldable-traversable
29+
- foreign
2930
- foreign-object
3031
- formatters
3132
- http-methods
3233
- httpurple
3334
- identity
3435
- integers
35-
- js-fetch
3636
- js-date
37-
- js-uri
37+
- js-fetch
3838
- js-promise-aff
39+
- js-uri
40+
- json
3941
- lists
4042
- maybe
41-
- media-types
4243
- newtype
4344
- node-buffer
4445
- node-child-process
@@ -56,7 +57,6 @@ package:
5657
- partial
5758
- prelude
5859
- profunctor
59-
- profunctor-lenses
6060
- record
6161
- refs
6262
- registry-foreign

app/src/App/API.purs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ module Registry.App.API
1414

1515
import Registry.App.Prelude
1616

17-
import Data.Argonaut.Parser as Argonaut.Parser
17+
import Codec.JSON.DecodeError as CJ.DecodeError
1818
import Data.Array as Array
1919
import Data.Array.NonEmpty as NEA
2020
import Data.Array.NonEmpty as NonEmptyArray
21-
import Data.Codec.Argonaut as CA
22-
import Data.Codec.Argonaut.Record as CA.Record
21+
import Data.Codec.JSON as CJ
22+
import Data.Codec.JSON.Record as CJ.Record
2323
import Data.DateTime (DateTime)
2424
import Data.Foldable (traverse_)
2525
import Data.FoldableWithIndex (foldMapWithIndex)
@@ -34,6 +34,7 @@ import Data.String.NonEmpty as NonEmptyString
3434
import Data.String.Regex as Regex
3535
import Effect.Aff as Aff
3636
import Effect.Ref as Ref
37+
import JSON as JSON
3738
import Node.ChildProcess.Types (Exit(..))
3839
import Node.FS.Aff as FS.Aff
3940
import Node.FS.Stats as FS.Stats
@@ -409,7 +410,7 @@ publish source payload = do
409410
Except.throw $ "Found a valid purs.json file in the package source, but it does not typecheck."
410411
Right _ -> case parseJson Manifest.codec string of
411412
Left err -> do
412-
Log.error $ "Failed to parse manifest: " <> CA.printJsonDecodeError err
413+
Log.error $ "Failed to parse manifest: " <> CJ.DecodeError.print err
413414
Except.throw $ "Found a purs.json file in the package source, but it could not be decoded."
414415
Right manifest -> do
415416
Log.debug $ "Read a valid purs.json manifest from the package source:\n" <> stringifyJson Manifest.codec manifest
@@ -604,10 +605,10 @@ publish source payload = do
604605
Comment.comment "Failed to prune dependencies for legacy package, continuing anyway..."
605606
else do
606607
Except.throw "purs graph failed; cannot verify unused dependencies."
607-
Right output -> case Argonaut.Parser.jsonParser output of
608+
Right output -> case JSON.parse output of
608609
Left parseErr -> Except.throw $ "Failed to parse purs graph output as JSON while finding unused dependencies: " <> parseErr
609-
Right json -> case CA.decode PursGraph.pursGraphCodec json of
610-
Left decodeErr -> Except.throw $ "Failed to decode JSON from purs graph output while finding unused dependencies: " <> CA.printJsonDecodeError decodeErr
610+
Right json -> case CJ.decode PursGraph.pursGraphCodec json of
611+
Left decodeErr -> Except.throw $ "Failed to decode JSON from purs graph output while finding unused dependencies: " <> CJ.DecodeError.print decodeErr
611612
Right graph -> do
612613
Log.debug "Got a valid graph of source and dependencies. Removing install dir and associating discovered modules with their packages..."
613614
FS.Extra.remove tmpDepsDir
@@ -1023,7 +1024,7 @@ publishToPursuit { packageSourceDir, dependenciesDir, compiler, resolutions } =
10231024
let lines = String.split (String.Pattern "\n") publishResult
10241025
case Array.last lines of
10251026
Nothing -> Except.throw "Publishing failed because of an unexpected compiler error. cc @purescript/packaging"
1026-
Just jsonString -> case Argonaut.Parser.jsonParser jsonString of
1027+
Just jsonString -> case JSON.parse jsonString of
10271028
Left err -> Except.throw $ String.joinWith "\n"
10281029
[ "Failed to parse output of publishing. cc @purescript/packaging"
10291030
, "```"
@@ -1042,8 +1043,8 @@ publishToPursuit { packageSourceDir, dependenciesDir, compiler, resolutions } =
10421043

10431044
type PursuitResolutions = Map RawPackageName { version :: Version, path :: FilePath }
10441045

1045-
pursuitResolutionsCodec :: JsonCodec PursuitResolutions
1046-
pursuitResolutionsCodec = rawPackageNameMapCodec $ CA.Record.object "Resolution" { version: Version.codec, path: CA.string }
1046+
pursuitResolutionsCodec :: CJ.Codec PursuitResolutions
1047+
pursuitResolutionsCodec = rawPackageNameMapCodec $ CJ.named "Resolution" $ CJ.Record.object { version: Version.codec, path: CJ.string }
10471048

10481049
-- Resolutions format: https://github.com/purescript/purescript/pull/3565
10491050
--

app/src/App/CLI/Licensee.purs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ module Registry.App.CLI.Licensee where
22

33
import Registry.App.Prelude
44

5+
import Codec.JSON.DecodeError as CJ.DecodeError
56
import Control.Parallel as Parallel
67
import Data.Array as Array
7-
import Data.Codec.Argonaut as CA
8-
import Data.Codec.Argonaut.Record as CA.Record
8+
import Data.Codec.JSON as CJ
9+
import Data.Codec.JSON.Record as CJ.Record
910
import Node.ChildProcess.Types (Exit(..))
1011
import Node.FS.Aff as FS
1112
import Node.Library.Execa as Execa
@@ -32,15 +33,15 @@ detect directory = do
3233
-- but we consider this valid Licensee output.
3334
Normally n | n == 0 || n == 1 -> do
3435
let
35-
parse :: String -> Either JsonDecodeError (Array String)
36-
parse str = map (map _.spdx_id <<< _.licenses) $ flip parseJson str $ CA.Record.object "Licenses"
37-
{ licenses: CA.array $ CA.Record.object "SPDXIds"
38-
{ spdx_id: CA.string }
36+
parse :: String -> Either CJ.DecodeError (Array String)
37+
parse str = map (map _.spdx_id <<< _.licenses) $ flip parseJson str $ CJ.named "Licenses" $ CJ.Record.object
38+
{ licenses: CJ.array $ CJ.named "SPDXIds" $ CJ.Record.object
39+
{ spdx_id: CJ.string }
3940
}
4041

4142
case parse result.stdout of
4243
Left error -> do
43-
let printedError = CA.printJsonDecodeError error
44+
let printedError = CJ.DecodeError.print error
4445
Left printedError
4546
Right out -> do
4647
-- A NOASSERTION result means that a LICENSE file could not be parsed.

app/src/App/CLI/Purs.purs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ module Registry.App.CLI.Purs where
22

33
import Registry.App.Prelude
44

5+
import Codec.JSON.DecodeError as CJ.DecodeError
56
import Data.Array as Array
6-
import Data.Codec.Argonaut as CA
7-
import Data.Codec.Argonaut.Compat as CA.Compat
8-
import Data.Codec.Argonaut.Record as CA.Record
7+
import Data.Codec.JSON as CJ
8+
import Data.Codec.JSON.Common as CJ.Common
9+
import Data.Codec.JSON.Record as CJ.Record
910
import Data.Foldable (foldMap)
1011
import Data.String as String
1112
import Node.ChildProcess.Types (Exit(..))
@@ -32,14 +33,14 @@ type CompilerError =
3233
, moduleName :: Maybe String
3334
}
3435

35-
compilerErrorCodec :: JsonCodec CompilerError
36-
compilerErrorCodec = CA.Record.object "CompilerError"
36+
compilerErrorCodec :: CJ.Codec CompilerError
37+
compilerErrorCodec = CJ.named "CompilerError" $ CJ.Record.object
3738
{ position: sourcePositionCodec
38-
, message: CA.string
39-
, errorCode: CA.string
40-
, errorLink: CA.string
41-
, filename: CA.string
42-
, moduleName: CA.Compat.maybe CA.string
39+
, message: CJ.string
40+
, errorCode: CJ.string
41+
, errorLink: CJ.string
42+
, filename: CJ.string
43+
, moduleName: CJ.Common.nullable CJ.string
4344
}
4445

4546
type SourcePosition =
@@ -49,12 +50,12 @@ type SourcePosition =
4950
, endColumn :: Int
5051
}
5152

52-
sourcePositionCodec :: JsonCodec SourcePosition
53-
sourcePositionCodec = CA.Record.object "SourcePosition"
54-
{ startLine: CA.int
55-
, startColumn: CA.int
56-
, endLine: CA.int
57-
, endColumn: CA.int
53+
sourcePositionCodec :: CJ.Codec SourcePosition
54+
sourcePositionCodec = CJ.named "SourcePosition" $ CJ.Record.object
55+
{ startLine: CJ.int
56+
, startColumn: CJ.int
57+
, endLine: CJ.int
58+
, endColumn: CJ.int
5859
}
5960

6061
-- TODO: This would be better handled with dodo-printer.
@@ -120,8 +121,8 @@ callCompiler compilerArgs = do
120121
$ String.replaceAll (String.Pattern ".") (String.Replacement "_")
121122
$ Version.print version
122123

123-
errorsCodec = CA.Record.object "CompilerErrors"
124-
{ errors: CA.array compilerErrorCodec
124+
errorsCodec = CJ.named "CompilerErrors" $ CJ.Record.object
125+
{ errors: CJ.array compilerErrorCodec
125126
}
126127

127128
result <- _.getResult =<< Execa.execa purs (printCommand compilerArgs.command) (_ { cwd = compilerArgs.cwd })
@@ -137,7 +138,7 @@ callCompiler compilerArgs = do
137138
Just version | Right min <- Version.parse "0.14.0", version < min -> result.stderr
138139
Just _ -> result.stdout
139140
case parseJson errorsCodec output of
140-
Left err -> UnknownError $ String.joinWith "\n" [ result.stdout, result.stderr, CA.printJsonDecodeError err ]
141+
Left err -> UnknownError $ String.joinWith "\n" [ result.stdout, result.stderr, CJ.DecodeError.print err ]
141142
Right ({ errors } :: { errors :: Array CompilerError })
142143
| Array.null errors -> UnknownError "Non-normal exit code, but no errors reported."
143144
| otherwise -> CompilationError errors

app/src/App/Effect/Cache.purs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ module Registry.App.Effect.Cache
3434

3535
import Registry.App.Prelude
3636

37-
import Data.Argonaut.Parser as Argonaut.Parser
38-
import Data.Codec.Argonaut as CA
37+
import Codec.JSON.DecodeError as CJ.DecodeError
38+
import Data.Codec.JSON as CJ
3939
import Data.Const (Const(..))
4040
import Data.Exists (Exists)
4141
import Data.Exists as Exists
@@ -45,6 +45,7 @@ import Data.String as String
4545
import Data.Symbol (class IsSymbol)
4646
import Effect.Aff as Aff
4747
import Effect.Ref as Ref
48+
import JSON as JSON
4849
import JSURI as JSURI
4950
import Node.FS.Aff as FS.Aff
5051
import Node.Path as Path
@@ -254,7 +255,7 @@ class FsEncodable key where
254255
-- | cache values as something other than JSON or a raw buffer.
255256
data FsEncoding :: (Type -> Type -> Type) -> Type -> Type -> Type
256257
data FsEncoding z b a
257-
= AsJson String (JsonCodec a) (z a b)
258+
= AsJson String (CJ.Codec a) (z a b)
258259
| AsBuffer String (z Buffer b)
259260

260261
-- | Handle the Cache effect by caching values on the file system, given a
@@ -286,13 +287,13 @@ getFsImpl cacheDir = case _ of
286287
Left _ -> do
287288
Log.debug $ "No cache file found for " <> id <> " at path " <> path
288289
pure $ reply Nothing
289-
Right content -> case Argonaut.Parser.jsonParser content of
290+
Right content -> case JSON.parse content of
290291
Left parseError -> do
291292
Log.error $ "Found cache file for " <> id <> " at path " <> path <> " but its contents are not valid JSON: " <> parseError
292293
deletePathById cacheDir id *> pure (reply Nothing)
293-
Right jsonContent -> case CA.decode codec jsonContent of
294+
Right jsonContent -> case CJ.decode codec jsonContent of
294295
Left decodeError -> do
295-
let error = CA.printJsonDecodeError decodeError
296+
let error = CJ.DecodeError.print decodeError
296297
Log.error $ "Found cache file for " <> id <> " at path " <> path <> " but its contents could not be decoded with the provided codec:\n" <> error
297298
deletePathById cacheDir id *> pure (reply Nothing)
298299
Right entry -> do

app/src/App/Effect/GitHub.purs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ module Registry.App.Effect.GitHub
1818

1919
import Registry.App.Prelude
2020

21-
import Data.Argonaut.Parser as Argonaut.Parser
22-
import Data.Codec.Argonaut as CA
23-
import Data.Codec.Argonaut.Common as CA.Common
24-
import Data.Codec.Argonaut.Record as CA.Record
21+
import Codec.JSON.DecodeError as CJ.DecodeError
22+
import Data.Codec.JSON as CJ
23+
import Data.Codec.JSON.Common as CJ.Common
24+
import Data.Codec.JSON.Record as CJ.Record
2525
import Data.DateTime (DateTime)
2626
import Data.DateTime as DateTime
2727
import Data.Exists as Exists
2828
import Data.HTTP.Method (Method(..))
2929
import Data.Time.Duration as Duration
3030
import Foreign.Object as Object
31+
import JSON as JSON
3132
import Registry.App.Effect.Cache (class FsEncodable, class MemoryEncodable, Cache, CacheRef, FsEncoding(..), MemoryEncoding(..))
3233
import Registry.App.Effect.Cache as Cache
3334
import Registry.App.Effect.Env (RESOURCE_ENV)
@@ -67,7 +68,7 @@ instance MemoryEncodable GitHubCache where
6768
instance FsEncodable GitHubCache where
6869
encodeFs = case _ of
6970
Request route next -> do
70-
let codec = CA.Common.either Octokit.githubErrorCodec requestResultCodec
71+
let codec = CJ.Common.either Octokit.githubErrorCodec requestResultCodec
7172
Exists.mkExists $ AsJson ("Request__" <> Octokit.printGitHubRoute route) codec next
7273

7374
data GitHub a
@@ -99,14 +100,14 @@ getContent :: forall r. Address -> RawVersion -> FilePath -> Run (GITHUB + r) (E
99100
getContent address (RawVersion ref) path = Run.lift _github (GetContent address ref path identity)
100101

101102
-- | Read the content of a JSON file in the provided repo, decoding its contents.
102-
getJsonFile :: forall r a. Address -> RawVersion -> JsonCodec a -> FilePath -> Run (GITHUB + r) (Either GitHubError a)
103+
getJsonFile :: forall r a. Address -> RawVersion -> CJ.Codec a -> FilePath -> Run (GITHUB + r) (Either GitHubError a)
103104
getJsonFile address ref codec path = do
104105
content <- getContent address ref path
105106
let
106-
attemptDecode inner = case Argonaut.Parser.jsonParser (JsonRepair.tryRepair inner) of
107+
attemptDecode inner = case JSON.parse (JsonRepair.tryRepair inner) of
107108
Left jsonError -> Left $ Octokit.DecodeError $ "Not Json: " <> jsonError
108-
Right json -> case CA.decode codec json of
109-
Left decodeError -> Left $ Octokit.DecodeError $ CA.printJsonDecodeError decodeError
109+
Right json -> case CJ.decode codec json of
110+
Left decodeError -> Left $ Octokit.DecodeError $ CJ.DecodeError.print decodeError
110111
Right decoded -> Right decoded
111112
pure $ attemptDecode =<< content
112113

@@ -178,7 +179,7 @@ request octokit githubRequest@{ route: route@(GitHubRoute method _ _), codec } =
178179
case entry of
179180
Nothing -> do
180181
result <- requestWithBackoff octokit githubRequest
181-
Cache.put _githubCache (Request route) (result <#> \response -> { modified: now, etag: Nothing, response: CA.encode codec response })
182+
Cache.put _githubCache (Request route) (result <#> \response -> { modified: now, etag: Nothing, response: CJ.encode codec response })
182183
pure result
183184

184185
Just cached -> case cached of
@@ -195,9 +196,9 @@ request octokit githubRequest@{ route: route@(GitHubRoute method _ _), codec } =
195196
Cache.delete _githubCache (Request route)
196197
request octokit githubRequest
197198

198-
Right prevResponse -> case CA.decode codec prevResponse.response of
199+
Right prevResponse -> case CJ.decode codec prevResponse.response of
199200
Left err -> do
200-
Log.debug $ "Could not decode previous response data using the provided codec: " <> CA.printJsonDecodeError err
201+
Log.debug $ "Could not decode previous response data using the provided codec: " <> CJ.DecodeError.print err
201202
Log.debug $ "This indicates an out-of-date cache entry. Clearing cache for route " <> printedRoute
202203
Cache.delete _githubCache (Request route)
203204
Log.debug "Retrying request..."
@@ -216,7 +217,7 @@ request octokit githubRequest@{ route: route@(GitHubRoute method _ _), codec } =
216217
Right decoded | Just etag <- prevResponse.etag -> do
217218
Log.debug $ "Found valid cache entry with etags for " <> printedRoute
218219
let headers = Object.insert "If-None-Match" etag githubRequest.headers
219-
Log.debug $ "Verifying cached status with headers: " <> stringifyJson (CA.Common.foreignObject CA.string) headers
220+
Log.debug $ "Verifying cached status with headers: " <> stringifyJson (CJ.Common.foreignObject CJ.string) headers
220221
let modifiedRequest = githubRequest { headers = headers }
221222
conditionalResponse <- requestWithBackoff octokit modifiedRequest
222223
case conditionalResponse of
@@ -234,7 +235,7 @@ request octokit githubRequest@{ route: route@(GitHubRoute method _ _), codec } =
234235
Cache.put _githubCache (Request route) (Left otherError)
235236
pure (Left otherError)
236237
Right valid -> do
237-
Cache.put _githubCache (Request route) (Right { response: CA.encode codec valid, modified: now, etag: Nothing })
238+
Cache.put _githubCache (Request route) (Right { response: CJ.encode codec valid, modified: now, etag: Nothing })
238239
pure $ Right valid
239240

240241
-- Since we don't have support for conditional requests via etags, we'll instead
@@ -244,7 +245,7 @@ request octokit githubRequest@{ route: route@(GitHubRoute method _ _), codec } =
244245
Right _ | DateTime.diff now prevResponse.modified >= Duration.Hours 4.0 -> do
245246
Log.debug $ "Found cache entry but it was modified more than 4 hours ago, refetching " <> printedRoute
246247
result <- requestWithBackoff octokit githubRequest
247-
Cache.put _githubCache (Request route) (result <#> \resp -> { response: CA.encode codec resp, modified: now, etag: Nothing })
248+
Cache.put _githubCache (Request route) (result <#> \resp -> { response: CJ.encode codec resp, modified: now, etag: Nothing })
248249
pure result
249250

250251
Right decoded -> do
@@ -285,12 +286,12 @@ requestWithBackoff octokit githubRequest = do
285286
type RequestResult =
286287
{ modified :: DateTime
287288
, etag :: Maybe String
288-
, response :: Json
289+
, response :: JSON
289290
}
290291

291-
requestResultCodec :: JsonCodec RequestResult
292-
requestResultCodec = CA.Record.object "RequestResult"
293-
{ etag: CA.Common.maybe CA.string
292+
requestResultCodec :: CJ.Codec RequestResult
293+
requestResultCodec = CJ.named "RequestResult" $ CJ.Record.object
294+
{ etag: CJ.Common.maybe CJ.string
294295
, modified: Internal.Codec.iso8601DateTime
295-
, response: CA.json
296+
, response: CJ.json
296297
}

0 commit comments

Comments
 (0)