@@ -9,7 +9,8 @@ import Ccap.Codegen.Parser.Export as Export
99import  Ccap.Codegen.Shared  (DelimitedLiteralDir (..), OutputSpec , delimitedLiteral , indented , modulesInScope )
1010import  Ccap.Codegen.Types  (Annotations , Exports , Module , ModuleName , Primitive (..), RecordProp , TRef , TopType (..), Type (..), TypeDecl (..), ValidatedModule , isRecord , typeDeclName , typeDeclTopType )
1111import  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 )
1314import  Control.Monad.Reader  (Reader , ask , asks , runReader )
1415import  Data.Array  ((:))
1516import  Data.Array  as  Array 
@@ -40,7 +41,7 @@ modulePath mod = (Export.toPath mod.exports.scalaPkg) <> ".scala"
4041oneModule  ::  ValidatedModule  ->  Box 
4142oneModule 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
326327needsQualifier  ::  ModuleName  ->  TypeDecl  ->  Boolean 
327- needsQualifier modName = not <<< isClassFile  modName
328+ needsQualifier modName = not <<< isPrimaryClass  modName
328329
329330packageAnnotation  ::  Module  ->  Maybe  String 
330331packageAnnotation = Annotations .field " scala" " package" 
@@ -347,17 +348,29 @@ decoder annots = case _ of
347348  Primitive  p ->  pure $ (text $ " Decoder" 
348349
349350jsonRef  ::  String  ->  String  ->  String 
350- jsonRef which typ = " json" 
351+ jsonRef which typ = " json"   --  should be blank if it is the primary class 
351352
352353jsonTypeRef  ::  String  ->  TRef  ->  Codegen  String 
353354jsonTypeRef 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
362375jsonList  ::  Box  ->  Box 
363376jsonList json = json <<>> text " .list" 
0 commit comments