Merge pull request #167 from quicktype/iranything
Rename IRNothing to IRAnything
This commit is contained in:
Коммит
87662c309d
|
@ -185,7 +185,7 @@ noForbidNamer namer = forbidNamer namer (const [])
|
|||
|
||||
typeNameForUnion :: IRGraph -> Map Int String -> IRType -> String
|
||||
typeNameForUnion graph classNames = case _ of
|
||||
IRNothing -> "anything"
|
||||
IRAnything -> "anything"
|
||||
IRNull -> "null"
|
||||
IRInteger -> "int"
|
||||
IRDouble -> "double"
|
||||
|
|
14
src/IR.purs
14
src/IR.purs
|
@ -103,7 +103,7 @@ unionWithDefault unifier default m1 m2 =
|
|||
|
||||
unifyClassDatas :: IRClassData -> IRClassData -> IR IRClassData
|
||||
unifyClassDatas (IRClassData { names: na, properties: pa }) (IRClassData { names: nb, properties: pb }) = do
|
||||
properties <- unionWithDefault unifyTypesWithNull IRNothing pa pb
|
||||
properties <- unionWithDefault unifyTypesWithNull IRAnything pa pb
|
||||
pure $ IRClassData { names: unifyNamed S.union na nb, properties }
|
||||
|
||||
unifyClassRefs :: Int -> Int -> IR Int
|
||||
|
@ -117,14 +117,14 @@ unifyClassRefs ia ib =
|
|||
combineClasses ia ib unified
|
||||
|
||||
unifyMaybes :: Maybe IRType -> Maybe IRType -> IR IRType
|
||||
unifyMaybes Nothing Nothing = pure IRNothing
|
||||
unifyMaybes Nothing Nothing = pure IRAnything
|
||||
unifyMaybes (Just a) Nothing = pure a
|
||||
unifyMaybes Nothing (Just b) = pure b
|
||||
unifyMaybes (Just a) (Just b) = unifyTypes a b
|
||||
|
||||
unifyTypes :: IRType -> IRType -> IR IRType
|
||||
unifyTypes IRNothing x = pure x
|
||||
unifyTypes x IRNothing = pure x
|
||||
unifyTypes IRAnything x = pure x
|
||||
unifyTypes x IRAnything = pure x
|
||||
unifyTypes IRInteger IRDouble = pure IRDouble
|
||||
unifyTypes IRDouble IRInteger = pure IRDouble
|
||||
unifyTypes (IRArray a) (IRArray b) = IRArray <$> unifyTypes a b
|
||||
|
@ -141,10 +141,10 @@ unifyTypes a b | a == b = pure a
|
|||
pure $ IRUnion u2
|
||||
|
||||
unifyMultipleTypes :: List IRType -> IR IRType
|
||||
unifyMultipleTypes = L.foldM unifyTypes IRNothing
|
||||
unifyMultipleTypes = L.foldM unifyTypes IRAnything
|
||||
|
||||
unifyTypesWithNull :: IRType -> IRType -> IR IRType
|
||||
unifyTypesWithNull IRNothing IRNothing = pure IRNothing
|
||||
unifyTypesWithNull IRAnything IRAnything = pure IRAnything
|
||||
unifyTypesWithNull a b = unifyTypes (nullifyNothing a) (nullifyNothing b)
|
||||
|
||||
unifySetOfClasses :: Set Int -> IR Unit
|
||||
|
@ -181,7 +181,7 @@ updateClasses classUpdater typeUpdater = do
|
|||
unifyWithUnion :: IRUnionRep -> IRType -> IR IRUnionRep
|
||||
unifyWithUnion u@(IRUnionRep { names, primitives, arrayType, classRef, mapType }) t =
|
||||
case t of
|
||||
IRNothing -> pure u
|
||||
IRAnything -> pure u
|
||||
IRNull -> addBit irUnion_Null
|
||||
IRInteger -> addBit irUnion_Integer
|
||||
IRDouble -> addBit irUnion_Double
|
||||
|
|
|
@ -148,9 +148,9 @@ irUnion_String = 32
|
|||
-- | and in any case is an implementation detail that should be hidden from
|
||||
-- | higher-level users like the language renderers.
|
||||
-- |
|
||||
-- | `IRNothing` was a bad design choice and will go away. See issue #56.
|
||||
-- | `IRAnything` was a bad design choice and will go away. See issue #56.
|
||||
data IRType
|
||||
= IRNothing
|
||||
= IRAnything
|
||||
| IRNull
|
||||
| IRInteger
|
||||
| IRDouble
|
||||
|
@ -219,15 +219,15 @@ isMap (IRMap _) = true
|
|||
isMap _ = false
|
||||
|
||||
nullifyNothing :: IRType -> IRType
|
||||
nullifyNothing IRNothing = IRNull
|
||||
nullifyNothing IRAnything = IRNull
|
||||
nullifyNothing x = x
|
||||
|
||||
canBeNull :: IRType -> Boolean
|
||||
canBeNull =
|
||||
case _ of
|
||||
IRNothing -> true
|
||||
IRAnything -> true
|
||||
IRNull -> true
|
||||
-- FIXME: shouldn't we check for IRNothing in union, too?
|
||||
-- FIXME: shouldn't we check for IRAnything in union, too?
|
||||
IRUnion (IRUnionRep { primitives }) -> (Bits.and primitives irUnion_Null) /= 0
|
||||
_ -> false
|
||||
|
||||
|
@ -263,7 +263,7 @@ isMaybeSubclassOfMaybe graph (Just a) (Just b) = isSubclassOf graph a b
|
|||
isMaybeSubclassOfMaybe _ _ _ = false
|
||||
|
||||
isSubtypeOf :: IRGraph -> IRType -> IRType -> Boolean
|
||||
isSubtypeOf _ IRNothing _ = true
|
||||
isSubtypeOf _ IRAnything _ = true
|
||||
isSubtypeOf graph (IRUnion a) (IRUnion b) =
|
||||
let IRUnionRep { primitives: pa, arrayType: aa, classRef: ca, mapType: ma } = a
|
||||
IRUnionRep { primitives: pb, arrayType: ab, classRef: cb, mapType: mb } = a
|
||||
|
@ -363,7 +363,7 @@ nullableFromUnion union =
|
|||
|
||||
forUnion_ :: forall m. Monad m => IRUnionRep -> (IRType -> m Unit) -> m Unit
|
||||
forUnion_ (IRUnionRep { primitives, arrayType, classRef, mapType }) f = do
|
||||
when (inPrimitives irUnion_Nothing) do f IRNothing
|
||||
when (inPrimitives irUnion_Nothing) do f IRAnything
|
||||
when (inPrimitives irUnion_Null) do f IRNull
|
||||
when (inPrimitives irUnion_Integer) do f IRInteger
|
||||
when (inPrimitives irUnion_Double) do f IRDouble
|
||||
|
@ -392,7 +392,7 @@ mapUnionM f (IRUnionRep { primitives, arrayType, classRef, mapType }) = do
|
|||
>>= mapPrimitive irUnion_Double IRDouble
|
||||
>>= mapPrimitive irUnion_Integer IRInteger
|
||||
>>= mapPrimitive irUnion_Null IRNull
|
||||
>>= mapPrimitive irUnion_Nothing IRNothing
|
||||
>>= mapPrimitive irUnion_Nothing IRAnything
|
||||
where
|
||||
mapPrimitive :: Int -> IRType -> List a -> m (List a)
|
||||
mapPrimitive bit t l =
|
||||
|
@ -417,7 +417,7 @@ unionToList = mapUnion id
|
|||
isUnionMember :: IRType -> IRUnionRep -> Boolean
|
||||
isUnionMember t (IRUnionRep { primitives, arrayType, classRef, mapType }) =
|
||||
case t of
|
||||
IRNothing -> inPrimitives irUnion_Nothing
|
||||
IRAnything -> inPrimitives irUnion_Nothing
|
||||
IRNull -> inPrimitives irUnion_Null
|
||||
IRInteger -> inPrimitives irUnion_Integer
|
||||
IRDouble -> inPrimitives irUnion_Double
|
||||
|
|
|
@ -83,7 +83,7 @@ renderUnionToCSharp ur =
|
|||
|
||||
renderTypeToCSharp :: IRType -> Doc String
|
||||
renderTypeToCSharp = case _ of
|
||||
IRNothing -> pure "object" -- we can have arrays of nothing
|
||||
IRAnything -> pure "object" -- we can have arrays of nothing
|
||||
IRNull -> pure "object"
|
||||
IRInteger -> pure "long"
|
||||
IRDouble -> pure "double"
|
||||
|
|
|
@ -190,7 +190,7 @@ parenIfNeeded { rendered, multiWord: true } = "(" <> rendered <> ")"
|
|||
|
||||
typeStringForType :: IRType -> Doc { rendered :: String, multiWord :: Boolean }
|
||||
typeStringForType = case _ of
|
||||
IRNothing -> singleWord "Jdec.Value"
|
||||
IRAnything -> singleWord "Jdec.Value"
|
||||
IRNull -> singleWord "()"
|
||||
IRInteger -> singleWord "Int"
|
||||
IRDouble -> singleWord "Float"
|
||||
|
@ -218,7 +218,7 @@ unionConstructorName unionName t = do
|
|||
|
||||
decoderNameForType :: IRType -> Doc { rendered :: String, multiWord :: Boolean }
|
||||
decoderNameForType = case _ of
|
||||
IRNothing -> singleWord "Jdec.value"
|
||||
IRAnything -> singleWord "Jdec.value"
|
||||
IRNull -> multiWord "Jdec.null" "()"
|
||||
IRInteger -> singleWord "Jdec.int"
|
||||
IRDouble -> singleWord "Jdec.float"
|
||||
|
@ -241,7 +241,7 @@ decoderNameForType = case _ of
|
|||
|
||||
encoderNameForType :: IRType -> Doc { rendered :: String, multiWord :: Boolean }
|
||||
encoderNameForType = case _ of
|
||||
IRNothing -> singleWord "identity"
|
||||
IRAnything -> singleWord "identity"
|
||||
IRNull -> multiWord "always" "Jenc.null"
|
||||
IRInteger -> singleWord "Jenc.int"
|
||||
IRDouble -> singleWord "Jenc.float"
|
||||
|
|
|
@ -77,7 +77,7 @@ renderUnionToGolang ur =
|
|||
|
||||
renderTypeToGolang :: IRType -> Doc { rendered :: String, comment :: Maybe String }
|
||||
renderTypeToGolang = case _ of
|
||||
IRNothing -> noComment "interface{}"
|
||||
IRAnything -> noComment "interface{}"
|
||||
IRNull -> noComment "interface{}"
|
||||
IRInteger -> noComment "int64"
|
||||
IRDouble -> noComment "float64"
|
||||
|
|
|
@ -93,7 +93,7 @@ renderUnionWithTypeRenderer typeRenderer ur =
|
|||
|
||||
renderType :: Boolean -> IRType -> Doc String
|
||||
renderType reference = case _ of
|
||||
IRNothing -> pure "Object"
|
||||
IRAnything -> pure "Object"
|
||||
IRNull -> pure "Object"
|
||||
IRInteger -> pure $ if reference then "Long" else "long"
|
||||
IRDouble -> pure $ if reference then "Double" else "double"
|
||||
|
|
|
@ -137,7 +137,7 @@ lookupRef root ref local@(JSONSchema { definitions }) =
|
|||
toIRAndUnify :: forall a f. Foldable f => (a -> IR IRType) -> f a -> IR IRType
|
||||
toIRAndUnify toIR l = do
|
||||
irs <- mapM toIR $ L.fromFoldable l
|
||||
foldM unifyTypes IRNothing irs
|
||||
foldM unifyTypes IRAnything irs
|
||||
|
||||
jsonSchemaToIR :: JSONSchema -> Named String -> JSONSchema -> IR IRType
|
||||
jsonSchemaToIR root name schema@(JSONSchema { definitions, ref, types, oneOf, properties, additionalProperties, items, required })
|
||||
|
@ -152,12 +152,12 @@ jsonSchemaToIR root name schema@(JSONSchema { definitions, ref, types, oneOf, pr
|
|||
| Just jss <- oneOf =
|
||||
toIRAndUnify (jsonSchemaToIR root name) jss
|
||||
| otherwise =
|
||||
pure IRNothing
|
||||
pure IRAnything
|
||||
|
||||
jsonSchemaListToIR :: forall t. Traversable t => Named String -> t JSONSchema -> IR IRType
|
||||
jsonSchemaListToIR name l = do
|
||||
irTypes <- mapM (\js -> jsonSchemaToIR js name js) l
|
||||
foldM unifyTypes IRNothing irTypes
|
||||
foldM unifyTypes IRAnything irTypes
|
||||
|
||||
jsonTypeToIR :: JSONSchema -> Named String -> JSONType -> JSONSchema -> IR IRType
|
||||
jsonTypeToIR root name jsonType (JSONSchema schema) =
|
||||
|
@ -174,9 +174,9 @@ jsonTypeToIR root name jsonType (JSONSchema schema) =
|
|||
Nothing ->
|
||||
case schema.additionalProperties of
|
||||
Left true ->
|
||||
pure $ IRMap IRNothing
|
||||
pure $ IRMap IRAnything
|
||||
Left false ->
|
||||
pure $ IRNothing
|
||||
pure $ IRAnything
|
||||
Right js -> do
|
||||
ir <- jsonSchemaToIR root singularName js
|
||||
pure $ IRMap ir
|
||||
|
@ -185,7 +185,7 @@ jsonTypeToIR root name jsonType (JSONSchema schema) =
|
|||
Just js -> do
|
||||
ir <- (jsonSchemaToIR root singularName) js
|
||||
pure $ IRArray ir
|
||||
Nothing -> pure $ IRArray IRNothing
|
||||
Nothing -> pure $ IRArray IRAnything
|
||||
JSONBoolean -> pure IRBool
|
||||
JSONString -> pure IRString
|
||||
JSONNull -> pure IRNull
|
||||
|
@ -237,7 +237,7 @@ typeJson s = fromObject <$> typeStrMap s
|
|||
strMapForType :: IRType -> Doc (StrMap Json)
|
||||
strMapForType t =
|
||||
case t of
|
||||
IRNothing -> pure $ SM.empty
|
||||
IRAnything -> pure $ SM.empty
|
||||
IRNull -> typeStrMap "null"
|
||||
IRInteger -> typeStrMap "integer"
|
||||
IRDouble -> typeStrMap "number"
|
||||
|
|
|
@ -65,7 +65,7 @@ renderUnion ur =
|
|||
|
||||
renderType :: IRType -> Doc String
|
||||
renderType = case _ of
|
||||
IRNothing -> pure "Any" -- we can have arrays of nothing
|
||||
IRAnything -> pure "Any" -- we can have arrays of nothing
|
||||
IRNull -> pure "Null"
|
||||
IRInteger -> pure "Integer"
|
||||
IRDouble -> pure "Double"
|
||||
|
|
|
@ -192,7 +192,7 @@ renderUnion ur =
|
|||
|
||||
renderType :: IRType -> Doc String
|
||||
renderType = case _ of
|
||||
IRNothing -> pure "Any?"
|
||||
IRAnything -> pure "Any?"
|
||||
IRNull -> pure "Any?"
|
||||
IRInteger -> pure "Int"
|
||||
IRDouble -> pure "Double"
|
||||
|
@ -222,7 +222,7 @@ convertAny (IRUnion ur) var =
|
|||
Nothing -> do
|
||||
name <- lookupUnionName ur
|
||||
pure $ name <> ".fromJson(" <> var <> ")"
|
||||
convertAny IRNothing var =
|
||||
convertAny IRAnything var =
|
||||
pure var
|
||||
convertAny IRBool var =
|
||||
pure $ "convertBool(" <> var <> ")"
|
||||
|
@ -269,7 +269,7 @@ convertToAny (IRUnion ur) var =
|
|||
pure $ var <> ".map({ " <> convertCode <> " }) ?? NSNull()"
|
||||
Nothing ->
|
||||
pure $ var <> ".any"
|
||||
convertToAny IRNothing var =
|
||||
convertToAny IRAnything var =
|
||||
pure $ var <> " ?? NSNull()"
|
||||
convertToAny IRNull var =
|
||||
pure $ "NSNull() as Any"
|
||||
|
|
|
@ -69,7 +69,7 @@ renderUnion ur =
|
|||
|
||||
renderType :: IRType -> Doc String
|
||||
renderType = case _ of
|
||||
IRNothing -> pure "any" -- we can have arrays of nothing
|
||||
IRAnything -> pure "any" -- we can have arrays of nothing
|
||||
IRNull -> pure "null"
|
||||
IRInteger -> pure "number"
|
||||
IRDouble -> pure "number"
|
||||
|
@ -198,7 +198,7 @@ markNullable name _ = name
|
|||
|
||||
renderTypeMapType :: IRType -> Doc String
|
||||
renderTypeMapType = case _ of
|
||||
IRNothing -> pure $ quote "undefined"
|
||||
IRAnything -> pure $ quote "undefined"
|
||||
IRNull -> pure $ quote "undefined"
|
||||
IRInteger -> pure $ quote "number"
|
||||
IRDouble -> pure $ quote "number"
|
||||
|
|
|
@ -73,7 +73,7 @@ replaceClassWithMap :: Int -> IR Unit
|
|||
replaceClassWithMap i = do
|
||||
IRClassData { names, properties } <- getClass i
|
||||
let types = M.values properties
|
||||
t <- L.foldM unifyTypes IRNothing types
|
||||
t <- L.foldM unifyTypes IRAnything types
|
||||
replaceClass i (IRMap t)
|
||||
|
||||
makeMaps :: IR Unit
|
||||
|
|
Загрузка…
Ссылка в новой задаче