@@ -30,6 +30,7 @@ import qualified Data.HashMap.Strict as Map
30import Data.Maybe
30import Data.Maybe
31import Data.Semigroup ((<>))
31import Data.Semigroup ((<>))
32import qualified Data.Text as Text
32import qualified Data.Text as Text
33import Gen.Formatting
34import Gen.Formatting
34import Gen.Types
35import Gen.Types
35import Prelude hiding (sum)
36import Prelude hiding (sum)
@@ -60,35 +61,47 @@ globalSchema :: Global -> Fix Schema -> AST Global
60globalSchema g = schema g Nothing
61globalSchema g = schema g Nothing
62localSchema :: Global -> Local -> Fix Schema -> AST Global
63localSchema :: Global -> Local -> Fix Schema -> AST Global
63−localSchema g l s = do
64+localSchema g l = schema g (Just l)
66+ -- -- unsafe due to depth first adding of a single schema's properties.
64− let r = globalise l
67+ -- let r = globalise l
65− p <- uses schemas (Map.member r)
68+ -- p <- uses schemas (Map.member r)
67− then schema g (Just l) s
70+ -- then schema g (Just l) s
68− else schema r Nothing s
71+ -- else schema r Nothing s
69− -- local schema, check if the local schema exists and then just add it as
72schema :: Global -> Maybe Local -> Fix Schema -> AST Global
73schema :: Global -> Maybe Local -> Fix Schema -> AST Global
73−schema g ml (Fix f) = go f >>= insert this
74+schema g ml (Fix f) = go (maybe g (reference g) ml) f >>= uncurry insert
76− this = maybe g (reference g) ml
78− go :: Schema (Fix Schema) -> AST (Schema Global)
76+ go :: Global -> Schema (Fix Schema) -> AST (Global, Schema Global)
79− go = \case
77+ go p = \case
80− SAny i a -> pure (SAny i a)
78+ SAny i a -> pure (p, SAny i a)
81− SRef i r -> pure (SRef i r)
79+ SRef i r -> pure (p, SRef i r)
82− SLit i l -> pure (SLit i l)
80+ SLit i l -> pure (p, SLit i l)
83− SEnm i e -> pure (SEnm i e)
81+ SEnm i e -> (,SEnm i e) <$> name i p ["Type", "Option"]
83+ u <- name i p ["List", "Array"]
84− SArr i a -> SArr i <$> array a
84+ (u,) . SArr i <$> array u a
86+ u <- name i p ["Schema", "Object"]
85− SObj i o -> SObj i <$> object o
87+ (u,) . SObj i <$> object u o
87− array (Arr e) =
89+ array p (Arr e) =
88− Arr <$> schema this (Just "item") (setRequired e)
90+ Arr <$> schema p (Just "item") (setRequired e)
90− object (Obj aps ps) =
92+ object p (Obj aps ps) =
91− Obj Nothing <$> Map.traverseWithKey (localSchema this) ps
93+ Obj Nothing <$> Map.traverseWithKey (localSchema p) ps
96+ | Just x <- i ^. iId = pure x
98+ e <- uses schemas (Map.lookup p)
100+ (Nothing, _) -> pure p
101+ (Just _, z:zs) -> name i (reference g z) zs
102+ (Just x, []) -> throwError $
103+ format ("Unable to generate name for: " % gid % ", " % shown % ", " % gid % "\n" % shown)
93globalParam :: Local -> Param (Fix Schema) -> AST (Param Global)
106globalParam :: Local -> Param (Fix Schema) -> AST (Param Global)
94globalParam l p = ($ p) $ case l of
107globalParam l p = ($ p) $ case l of
@@ -126,15 +139,15 @@ method :: Map Local (Param Global)
126 -> Method (Fix Schema)
139 -> Method (Fix Schema)
127 -> AST (Method Global)
140 -> AST (Method Global)
128method qs suf m@Method {..} = do
141method qs suf m@Method {..} = do
129− ps <- Map.traverseWithKey (localParam _mId) _mParameters
142+ ps <- Map.traverseWithKey (localParam (abbreviate _mId)) _mParameters
130 cn <- use sCanonicalName
143 cn <- use sCanonicalName
132 let (_, typ, _) = mname cn suf _mId
145 let (_, typ, _) = mname cn suf _mId
134 b <- body typ
147 b <- body typ
136− let params = ps <> qs
149+ let params = ps <> qs
137− fields = Map.delete "alt" $ b (upload (Map.map _pParam params))
150+ fields = Map.delete "alt" $ b (upload (Map.map _pParam params))
139 void $ insert typ (SObj schemaInfo (Obj Nothing fields))
152 void $ insert typ (SObj schemaInfo (Obj Nothing fields))
140 pure $! m { _mParameters = params }
153 pure $! m { _mParameters = params }
@@ -177,7 +190,7 @@ insert g s = do
177 schemas %= Map.insert g s
190 schemas %= Map.insert g s
180− exists n x =
193+ exists n s' =
181 format ("Schema exists: " % stext % " - " % gid %
194 format ("Schema exists: " % stext % " - " % gid %
182− "\n\n[Current]\n" % shown % "\n[Tried]\n\n" % shown)
195+ "\n\n[Current]\n" % shown % "\n\n[Tried]\n" % shown % "\n\n")
183− n g x s
196+ n g s' s