Tidying runDoc call
Original commit f94bde27ce59c15c811649317a9217b906480328
This commit is contained in:
Родитель
1b4155cf4a
Коммит
901898d749
|
@ -26,7 +26,7 @@ import Utils (removeElement)
|
|||
type CSDoc = Doc Unit
|
||||
|
||||
forbiddenNames :: Array String
|
||||
forbiddenNames = [ "Converter", "JsonConverter", "Type" ]
|
||||
forbiddenNames = ["Converter", "JsonConverter", "Type"]
|
||||
|
||||
renderer :: Renderer
|
||||
renderer =
|
||||
|
@ -36,24 +36,33 @@ renderer =
|
|||
, render: renderGraphToCSharp
|
||||
}
|
||||
|
||||
transforms :: RendererTransformations
|
||||
transforms = {
|
||||
nameForClass,
|
||||
unionName,
|
||||
unionPredicate,
|
||||
nextNameToTry: \s -> "Other" <> s,
|
||||
forbiddenNames
|
||||
}
|
||||
|
||||
renderGraphToCSharp :: IRGraph -> String
|
||||
renderGraphToCSharp graph =
|
||||
runDoc csharpDoc nameForClass unionName unionPredicate nextNameToTry (S.fromFoldable forbiddenNames) graph unit
|
||||
where
|
||||
unionPredicate =
|
||||
case _ of
|
||||
IRUnion ur ->
|
||||
let s = unionToSet ur
|
||||
in
|
||||
if isNothing $ nullableFromSet s then
|
||||
Just s
|
||||
else
|
||||
Nothing
|
||||
_ -> Nothing
|
||||
nameForClass (IRClassData { names }) =
|
||||
csNameStyle $ combineNames names
|
||||
nextNameToTry s =
|
||||
"Other" <> s
|
||||
renderGraphToCSharp graph = runDoc csharpDoc transforms graph unit
|
||||
|
||||
unionPredicate :: IRType -> Maybe (Set IRType)
|
||||
unionPredicate = case _ of
|
||||
IRUnion ur ->
|
||||
let s = unionToSet ur
|
||||
in
|
||||
if isNothing $ nullableFromSet s then
|
||||
Just s
|
||||
else
|
||||
Nothing
|
||||
_ -> Nothing
|
||||
|
||||
|
||||
nameForClass :: IRClassData -> String
|
||||
nameForClass (IRClassData { names }) =
|
||||
csNameStyle $ combineNames names
|
||||
|
||||
unionName :: List String -> String
|
||||
unionName s =
|
||||
|
|
20
src/Doc.purs
20
src/Doc.purs
|
@ -22,6 +22,7 @@ module Doc
|
|||
import IR
|
||||
import IRGraph
|
||||
import Prelude
|
||||
import Types
|
||||
|
||||
import Control.Monad.RWS (RWS, evalRWS, asks, gets, modify, tell)
|
||||
import Data.Foldable (for_, intercalate, sequence_)
|
||||
|
@ -45,18 +46,17 @@ derive newtype instance applicativeDoc :: Applicative (Doc r)
|
|||
derive newtype instance bindDoc :: Bind (Doc r)
|
||||
derive newtype instance monadDoc :: Monad (Doc r)
|
||||
|
||||
runDoc :: forall r a. Doc r a -> (IRClassData -> String) -> (List String -> String) -> (IRType -> Maybe (Set IRType)) -> (String -> String) -> (Set String) -> IRGraph -> r -> String
|
||||
runDoc (Doc w) nameForClass unionName unionPredicate nextNameToTry forbiddenNames graph rendererInfo =
|
||||
runDoc :: forall r a. Doc r a -> RendererTransformations -> IRGraph -> r -> String
|
||||
runDoc (Doc w) t graph rendererInfo =
|
||||
let classes = classesInGraph graph
|
||||
classNames = transformNames nameForClass nextNameToTry forbiddenNames classes
|
||||
unions = L.fromFoldable $ filterTypes unionPredicate graph
|
||||
forbiddenForUnions = S.union forbiddenNames $ S.fromFoldable $ M.values classNames
|
||||
unionNames = transformNames nameForUnion nextNameToTry forbiddenForUnions $ map (\s -> Tuple s s) unions
|
||||
forbidden = S.fromFoldable t.forbiddenNames
|
||||
classNames = transformNames t.nameForClass t.nextNameToTry forbidden classes
|
||||
unions = L.fromFoldable $ filterTypes t.unionPredicate graph
|
||||
forbiddenForUnions = S.union forbidden $ S.fromFoldable $ M.values classNames
|
||||
nameForUnion s = t.unionName $ map (typeNameForUnion graph) $ L.sort $ L.fromFoldable s
|
||||
unionNames = transformNames nameForUnion t.nextNameToTry forbiddenForUnions $ map (\s -> Tuple s s) unions
|
||||
in
|
||||
evalRWS w { graph, classNames, unionNames, unions, rendererInfo } { indent: 0 } # snd
|
||||
where
|
||||
nameForUnion s =
|
||||
unionName $ map (typeNameForUnion graph) $ L.sort $ L.fromFoldable s
|
||||
evalRWS w { graph, classNames, unionNames, unions, rendererInfo } { indent: 0 } # snd
|
||||
|
||||
typeNameForUnion :: IRGraph -> IRType -> String
|
||||
typeNameForUnion graph = case _ of
|
||||
|
|
|
@ -32,25 +32,35 @@ renderer =
|
|||
, render: renderGraphToGolang
|
||||
}
|
||||
|
||||
transforms :: RendererTransformations
|
||||
transforms = {
|
||||
nameForClass,
|
||||
unionName,
|
||||
unionPredicate,
|
||||
nextNameToTry: \s -> "Other" <> s,
|
||||
forbiddenNames: []
|
||||
}
|
||||
|
||||
renderGraphToGolang :: IRGraph -> String
|
||||
renderGraphToGolang graph =
|
||||
runDoc golangDoc nameForClass unionName unionPredicate nextNameToTry S.empty graph unit
|
||||
where
|
||||
unionPredicate =
|
||||
case _ of
|
||||
IRUnion ur ->
|
||||
let s = unionToSet ur
|
||||
in
|
||||
if isNothing $ nullableFromSet s then
|
||||
Just s
|
||||
else
|
||||
Nothing
|
||||
_ -> Nothing
|
||||
nameForClass (IRClassData { names }) = goNameStyle $ combineNames names
|
||||
unionName components =
|
||||
"OneOf" <> (goNameStyle $ intercalate "_" $ components)
|
||||
nextNameToTry s =
|
||||
"Other" <> s
|
||||
renderGraphToGolang graph = runDoc golangDoc transforms graph unit
|
||||
|
||||
unionPredicate :: IRType -> Maybe (Set IRType)
|
||||
unionPredicate = case _ of
|
||||
IRUnion ur ->
|
||||
let s = unionToSet ur
|
||||
in
|
||||
if isNothing $ nullableFromSet s then
|
||||
Just s
|
||||
else
|
||||
Nothing
|
||||
_ -> Nothing
|
||||
|
||||
nameForClass :: IRClassData -> String
|
||||
nameForClass (IRClassData { names }) = goNameStyle $ combineNames names
|
||||
|
||||
unionName :: L.List String -> String
|
||||
unionName components =
|
||||
"OneOf" <> (goNameStyle $ intercalate "_" $ components)
|
||||
|
||||
isValueType :: IRType -> Boolean
|
||||
isValueType IRInteger = true
|
||||
|
|
|
@ -2,14 +2,23 @@ module Types where
|
|||
|
||||
import Prelude
|
||||
|
||||
import Data.List as L
|
||||
import Data.Set (Set())
|
||||
import Data.List (List())
|
||||
import Data.Maybe (Maybe())
|
||||
|
||||
import IRGraph
|
||||
import Doc
|
||||
|
||||
type Renderer = {
|
||||
name :: String,
|
||||
extension :: String,
|
||||
aceMode :: String,
|
||||
render :: IRGraph -> String
|
||||
}
|
||||
|
||||
type RendererTransformations = {
|
||||
nameForClass :: IRClassData -> String,
|
||||
unionName :: List String -> String,
|
||||
unionPredicate :: IRType -> Maybe (Set IRType),
|
||||
nextNameToTry :: String -> String,
|
||||
forbiddenNames :: Array String
|
||||
}
|
Загрузка…
Ссылка в новой задаче