Skip to content

Commit 6b5dc9d

Browse files
authored
Merge pull request #69 from ptwales/ref-jsonEncoder
Fix encoders referncing the primary classes encoder
2 parents 3063629 + e387b51 commit 6b5dc9d

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/Ccap/Codegen/Scala.purs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Ccap.Codegen.Parser.Export as Export
99
import Ccap.Codegen.Shared (DelimitedLiteralDir(..), OutputSpec, delimitedLiteral, indented, modulesInScope)
1010
import Ccap.Codegen.Types (Annotations, Exports, Module, ModuleName, Primitive(..), RecordProp, TRef, TopType(..), Type(..), TypeDecl(..), ValidatedModule, isRecord, typeDeclName, typeDeclTopType)
1111
import Ccap.Codegen.Util (fromMaybeT, maybeT)
12-
import Control.Monad.Maybe.Trans (MaybeT(..))
12+
import Control.Alt (alt)
13+
import Control.Monad.Maybe.Trans (MaybeT(..), runMaybeT)
1314
import Control.Monad.Reader (Reader, ask, asks, runReader)
1415
import Data.Array ((:))
1516
import Data.Array as Array
@@ -40,7 +41,7 @@ modulePath mod = (Export.toPath mod.exports.scalaPkg) <> ".scala"
4041
oneModule :: ValidatedModule -> Box
4142
oneModule mod = do
4243
let
43-
modDecl = classFileType mod
44+
modDecl = primaryClass mod
4445

4546
env =
4647
{ defaultPrefix: Nothing
@@ -317,14 +318,14 @@ externalTypeRef importedModule importedType =
317318
else
318319
typeName
319320

320-
classFileType :: forall r. { name :: ModuleName, types :: Array TypeDecl | r } -> Maybe TypeDecl
321-
classFileType { name, types } = Array.find (isClassFile name) types
321+
primaryClass :: forall r. { name :: ModuleName, types :: Array TypeDecl | r } -> Maybe TypeDecl
322+
primaryClass { name, types } = Array.find (isPrimaryClass name) types
322323

323-
isClassFile :: ModuleName -> TypeDecl -> Boolean
324-
isClassFile modName typeD = modName == typeDeclName typeD && (isRecord $ typeDeclTopType typeD)
324+
isPrimaryClass :: ModuleName -> TypeDecl -> Boolean
325+
isPrimaryClass modName typeD = modName == typeDeclName typeD && (isRecord $ typeDeclTopType typeD)
325326

326327
needsQualifier :: ModuleName -> TypeDecl -> Boolean
327-
needsQualifier modName = not <<< isClassFile modName
328+
needsQualifier modName = not <<< isPrimaryClass modName
328329

329330
packageAnnotation :: Module -> Maybe String
330331
packageAnnotation = Annotations.field "scala" "package" <<< _.annots
@@ -347,17 +348,29 @@ decoder annots = case _ of
347348
Primitive p -> pure $ (text $ "Decoder" <> jsonPrimitive p) <<>> decoderValidations annots
348349

349350
jsonRef :: String -> String -> String
350-
jsonRef which typ = "json" <> which <> typ
351+
jsonRef which typ = "json" <> which <> typ -- should be blank if it is the primary class
351352

352353
jsonTypeRef :: String -> TRef -> Codegen String
353354
jsonTypeRef which { mod, typ } =
354-
fromMaybeT (jsonRef which typ) do
355-
modName <- maybeT mod
356-
extMod <- MaybeT $ askModule modName
357-
extTypeDecl <- maybeT $ lookupTypeDecl typ extMod
358-
let
359-
path = Array.snoc (Array.fromFoldable $ packageAnnotation extMod) modName
360-
pure $ prefix path $ jsonRef which $ guard (needsQualifier modName extTypeDecl) typ
355+
let
356+
externalJson =
357+
runMaybeT do
358+
modName <- maybeT mod
359+
extMod <- MaybeT $ askModule modName
360+
extTypeDecl <- maybeT $ lookupTypeDecl typ extMod
361+
let
362+
path = Array.snoc (Array.fromFoldable $ packageAnnotation extMod) modName
363+
pure $ prefix path $ jsonRef which $ guard (needsQualifier modName extTypeDecl) typ
364+
365+
internalJson =
366+
runMaybeT do
367+
thisMod <- MaybeT $ Just <$> asks _.currentModule
368+
decl <- maybeT $ lookupTypeDecl typ thisMod
369+
pure $ jsonRef which $ guard (needsQualifier thisMod.name decl) typ
370+
371+
default = jsonRef which typ
372+
in
373+
alt <$> internalJson <*> externalJson <#> fromMaybe default
361374

362375
jsonList :: Box -> Box
363376
jsonList json = json <<>> text ".list"

test/Ccap/Codegen/Prefix.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ specs =
3333
$ check "prefixInternalRef:" " prefixInternalRef: Prefix.CustomType,"
3434
it "Don't need prefixes if defined in the companion object"
3535
$ check "customInternalRef:" " customInternalRef: CustomType,"
36+
it "Don't use the type suffix for encoders if they are the file's class"
37+
$ check "\"prefix\" ->" " \"prefix\" -> jsonEncoder.encode(x.prefix),"
3638
describe "type references" do
3739
it "Are defined in the companion object and don't need prefixes"
3840
$ check "type InternalRef" " type InternalRef = CustomType"

test/resources/prefix/Prefix.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ type CustomRec: {
2020
type Prefix: {
2121
prefixInternalRef: CustomType
2222
}
23+
24+
type SelfRef: {
25+
prefix: Prefix
26+
}

0 commit comments

Comments
 (0)