Skip to content

Commit 080fbac

Browse files
committed
set null array values to empty
1 parent 54379b2 commit 080fbac

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

generator/IDL/AST.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ data Type
4343
| Concrete
4444
{ typeName :: String
4545
, typeIsArray :: Bool
46-
, typeIsMaybe :: Bool
46+
, typeIsMaybe' :: Bool
4747
}
4848
deriving Show
4949

@@ -67,3 +67,7 @@ webglContext = Arg (Concrete "WebGLContext" False False) "webgl"
6767

6868
funcArgs :: Decl -> [Arg]
6969
funcArgs f = webglContext : methodArgs f
70+
71+
typeIsMaybe :: Type -> Bool
72+
typeIsMaybe t@Concrete{} = typeIsMaybe' t && not (typeIsArray t)
73+
typeIsMaybe _ = False

generator/IDL/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ parseType = typ PP.<?> "expecting type"
179179
else Concrete
180180
{ typeName = name
181181
, typeIsArray = isArray
182-
, typeIsMaybe = isMaybe
182+
, typeIsMaybe' = isMaybe
183183
}
184184

185185
parseArg :: Parse Arg

generator/IDL/Printer.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ ppRunFunc f@Function{} = ppRunTypeSig f $+$ ppRunFuncBody f
141141
ppRunFuncBody :: Decl -> Doc
142142
ppRunFuncBody f@Function { methodName = name, methodRetType = retType } =
143143
text name <+> args <+> "=" <+>
144-
(if ppAsMaybe retType then "toMaybe $" else empty) <+>
144+
safetyFn retType <+>
145145
"runFn" <> int (length $ funcArgs f) <+>
146146
implName f <+> args
147147
where
@@ -195,7 +195,7 @@ ppConvertType _ = empty
195195
ppConvertMaybeType :: Type -> Doc
196196
ppConvertMaybeType t@Concrete{} = wrapMaybe $ ppConvertType t
197197
where
198-
wrapMaybe typ = if ppAsMaybe t then parens ("Maybe" <+> typ) else typ
198+
wrapMaybe name = if typeIsMaybe t then parens ("Maybe" <+> name) else name
199199
ppConvertMaybeType _ = empty
200200

201201
ppExportList :: [Decl] -> Doc
@@ -252,6 +252,8 @@ toCamelCase = text . foldr go ""
252252
go '_' (l:ls) = toUpper l : ls
253253
go l ls = toLower l : ls
254254

255-
ppAsMaybe :: Type -> Bool
256-
ppAsMaybe t@Concrete{} = typeIsMaybe t && not (typeIsArray t)
257-
ppAsMaybe _ = False
255+
safetyFn :: Type -> Doc
256+
safetyFn t@Concrete{}
257+
| typeIsMaybe t = "toMaybe $"
258+
| typeIsArray t = "nullAsEmpty $"
259+
safetyFn _ = empty

0 commit comments

Comments
 (0)