Skip to content

Commit

Permalink
Merge pull request #143 from OxfordAbstracts/lower-case-symbols-only
Browse files Browse the repository at this point in the history
Lower case symbols only
  • Loading branch information
roryc89 authored Feb 13, 2025
2 parents fe24db5 + 944e6bb commit ea3adfe
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 106 deletions.
12 changes: 10 additions & 2 deletions codegen/schema/src/GraphQL/Client/CodeGen/GetSymbols.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Data.GraphQL.AST as AST
import Data.List (List, filter, foldMap, nub, sort, (:))
import Data.Maybe (maybe)
import Data.Newtype (unwrap)
import Data.String as String
import Data.String (toLower)

symbolsToCode :: forall f. Foldable f => String -> f String -> String
symbolsToCode modulePrefix symbols =
Expand All @@ -24,15 +26,16 @@ import Type.Proxy (Proxy(..))
symbols
# Array.fromFoldable
# Array.nub
# Array.filter (lower1stChar && not keyword)
# foldMap
( \s -> "\n" <> s <> " = Proxy :: Proxy" <> show s
( \s -> "\n" <> s <> " = Proxy :: Proxy " <> show s
)

getSymbols :: AST.Document -> List String
getSymbols doc =
unwrap doc
>>= definitionToSymbols
# filter (not keyword)
# filter (lower1stChar && not keyword)
# nub
# sort
where
Expand Down Expand Up @@ -82,3 +85,8 @@ getSymbols doc =

keyword :: String -> Boolean
keyword = flip elem [ "data", "type", "instance", "if", "then", "else" ]

lower1stChar :: String -> Boolean
lower1stChar s = head == toLower head
where
head = String.take 1 s
19 changes: 4 additions & 15 deletions codegen/schema/src/GraphQL/Client/CodeGen/SchemaCst.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Prelude
import Control.Monad.Writer (tell)
import Data.Array (elem)
import Data.Array as Array
import Data.CodePoint.Unicode (isLower)
import Data.Filterable (class Filterable, filter)
import Data.GraphQL.AST (NamedType)
import Data.GraphQL.AST as AST
Expand All @@ -18,9 +17,8 @@ import Data.Map (Map, lookup)
import Data.Map as Map
import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (unwrap, wrap)
import Data.String (codePointFromChar, toLower)
import Data.String (toLower)
import Data.String as String
import Data.String.CodeUnits (charAt)
import Data.String.Extra (pascalCase)
import Data.Traversable (class Foldable, class Traversable, for, traverse)
import Data.Tuple (Tuple(..))
Expand All @@ -29,13 +27,12 @@ import Data.Unfoldable (none)
import GraphQL.Client.CodeGen.Types (InputOptions, GqlEnum)
import GraphQL.Client.CodeGen.UtilCst (qualifiedTypeToName)
import Partial.Unsafe (unsafePartial)
import PureScript.CST.Types (ImportDecl, Module(..), ModuleHeader(..), ModuleName(..), Proper, QualifiedName)
import PureScript.CST.Types (ImportDecl, Module(..), ModuleHeader(..), ModuleName, Proper, QualifiedName)
import PureScript.CST.Types as CST
import Tidy.Codegen (declDerive, declNewtype, declType, docComments, leading, lineComments, typeApp, typeArrow, typeCtor, typeRecord, typeRecordEmpty, typeRow, typeString, typeWildcard)
import Tidy.Codegen.Class (class OverLeadingComments, toQualifiedName)
import Tidy.Codegen.Monad (CodegenT, codegenModule, importFrom, importType)
import Tidy.Util (nameOf)
import Unsafe.Coerce (unsafeCoerce)

gqlToPursSchema :: InputOptions -> String -> String -> AST.Document -> Array GqlEnum -> Module Void
gqlToPursSchema
Expand Down Expand Up @@ -192,7 +189,7 @@ gqlToPursSchema
, argumentsDefinition
, type: tipe
}
) = Tuple (safeFieldname name) case argumentsDefinition of
) = Tuple name case argumentsDefinition of
Nothing -> pursType
Just def ->
[ argumentsDefinitionToPurs objectName name def
Expand Down Expand Up @@ -224,7 +221,7 @@ gqlToPursSchema
, name
, type: tipe
}
) = Tuple (safeFieldname name) $ comment description
) = Tuple name $ comment description
case lookupOverride objectName name of
Nothing -> argTypeToPurs objectName fieldName name tipe
Just out -> case tipe of
Expand Down Expand Up @@ -465,14 +462,6 @@ getDefaultTypeNames { id, json, dateTime } = Map.fromFoldable
, "bool" /\ qualify "Boolean"
]

safeFieldname :: String -> String
safeFieldname s = if isSafe then s else show s
where
isSafe =
charAt 0 s
# maybe false \c ->
c == '_' || (isLower $ codePointFromChar c)

getTypeName :: AST.Type -> AST.NamedType
getTypeName = case _ of
AST.Type_NamedType n -> n
Expand Down
Loading

0 comments on commit ea3adfe

Please sign in to comment.