Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen: Rename some enum stuff for clarity #911

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions gen/src/Gen/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Gen.Text
renameOperation,
renameServiceFunction,
renameService,
renameBranch,
renameEnumValue,
renameReserved,
lowerHead,
upperHead,
Expand Down Expand Up @@ -54,8 +54,8 @@ renameService =
. stripPrefix "Service"
. stripSuffix "SDK"

renameBranch :: Text -> (Text, Text)
renameBranch = first go . join (,)
renameEnumValue :: Text -> (Text, Text)
renameEnumValue v = (go v, v)
where
go = Text.map (\c -> if Char.isAlphaNum c then c else '_') . upperHead

Expand Down Expand Up @@ -129,16 +129,16 @@ toCamelCase = toCamelCase' . Text.split (not . Char.isAlphaNum) . Text.dropWhile
lowerHead :: Text -> Text
lowerHead text
| Text.all (\c -> Char.isUpper c || Char.isDigit c) text =
Text.toLower text
Text.toLower text
| otherwise =
fromMaybe (mapHead Char.toLower text)
. msum
$ map
(\acronym -> mappend (Text.toLower acronym) <$> Text.stripPrefix acronym text)
[ "KMS",
"DB",
"MFA"
]
fromMaybe (mapHead Char.toLower text)
. msum
$ map
(\acronym -> mappend (Text.toLower acronym) <$> Text.stripPrefix acronym text)
[ "KMS",
"DB",
"MFA"
]

upperHead :: Text -> Text
upperHead = mapHead Char.toUpper
Expand Down
9 changes: 5 additions & 4 deletions gen/src/Gen/Types/Service.hs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ instance FromJSON (ShapeF ()) where
parseJSON = Aeson.withObject "shape" $ \o -> do
i <- Aeson.parseJSON (Aeson.Object o)
t <- o .: "type"
m <- o .:? "enum"
enum <- o .:? "enum"
ts <- o .:? "timestampFormat"

case t of
Expand All @@ -338,9 +338,10 @@ instance FromJSON (ShapeF ()) where
"boolean" -> pure (Lit i Bool)
"timestamp" -> pure (Lit i (Time ts))
"json" -> pure (Lit i Json)
"string" -> pure (maybe (Lit i Text) f m)
where
f = Enum i . HashMap.fromList . map (first mkId . renameBranch)
"string" -> pure $ case enum of
Nothing -> Lit i Text
Just values ->
Enum i . HashMap.fromList $ map (first mkId . renameEnumValue) values
_ -> fail $ "Unknown Shape type: " ++ Text.unpack t

data Operation f a b = Operation
Expand Down