Skip to content

Commit fc0e026

Browse files
authored
Merge pull request #273 from wolfadex/simplify-oneOf-name-generation
Simplify `oneOf` name generation
2 parents 1e1e793 + 3d9c363 commit fc0e026

1 file changed

Lines changed: 120 additions & 38 deletions

File tree

src/SchemaUtils.elm

Lines changed: 120 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,31 +1354,126 @@ subschemaToEnumMaybe subSchema =
13541354
|> Ok
13551355

13561356

1357-
typeToOneOfVariant :
1358-
{ type_ : Common.Type, documentation : Maybe String }
1359-
-> CliMonad (Maybe { name : Common.UnsafeName, type_ : Common.Type, documentation : Maybe String })
1357+
type alias VariantInfo =
1358+
{ name : Common.UnsafeName
1359+
, type_ : Common.Type
1360+
, documentation : Maybe String
1361+
}
1362+
1363+
1364+
typeToOneOfVariant : { type_ : Common.Type, documentation : Maybe String } -> CliMonad (Maybe VariantInfo)
13601365
typeToOneOfVariant { type_, documentation } =
1361-
type_
1362-
|> typeToAnnotationWithNullable
1363-
|> CliMonad.map
1364-
(\ann ->
1365-
let
1366-
rawName : String
1367-
rawName =
1368-
ann
1369-
|> Elm.ToString.annotation
1370-
|> .signature
1371-
in
1372-
if String.contains "{" rawName then
1373-
Nothing
1366+
let
1367+
wrap : String -> Common.Type -> CliMonad (Maybe VariantInfo)
1368+
wrap label t =
1369+
typeToOneOfVariant { type_ = t, documentation = documentation }
1370+
|> CliMonad.map
1371+
(Maybe.map
1372+
(\inner ->
1373+
{ name = Common.UnsafeName (label ++ Common.unwrapUnsafe inner.name)
1374+
, type_ = type_
1375+
, documentation = documentation
1376+
}
1377+
)
1378+
)
13741379

1375-
else
1376-
Just
1377-
{ name = Common.UnsafeName rawName
1378-
, type_ = type_
1379-
, documentation = documentation
1380-
}
1381-
)
1380+
simple : Common.UnsafeName -> CliMonad (Maybe VariantInfo)
1381+
simple name =
1382+
{ name = name
1383+
, type_ = type_
1384+
, documentation = documentation
1385+
}
1386+
|> Just
1387+
|> CliMonad.succeed
1388+
1389+
fromAnnotation : Elm.Annotation.Annotation -> CliMonad (Maybe VariantInfo)
1390+
fromAnnotation ann =
1391+
let
1392+
rawName : String
1393+
rawName =
1394+
ann
1395+
|> Elm.ToString.annotation
1396+
|> .signature
1397+
in
1398+
if String.contains "{" rawName then
1399+
Nothing
1400+
|> CliMonad.succeed
1401+
1402+
else
1403+
{ name = Common.UnsafeName rawName
1404+
, type_ = type_
1405+
, documentation = documentation
1406+
}
1407+
|> Just
1408+
|> CliMonad.succeed
1409+
in
1410+
case type_ of
1411+
Common.Nullable t ->
1412+
wrap "Nullable" t
1413+
1414+
Common.List t ->
1415+
wrap "List" t
1416+
1417+
Common.Dict additionalProperties [] ->
1418+
wrap "Dict" additionalProperties.type_
1419+
1420+
Common.Dict _ _ ->
1421+
CliMonad.succeed Nothing
1422+
1423+
Common.Object _ ->
1424+
CliMonad.succeed Nothing
1425+
1426+
Common.Null ->
1427+
simple (Common.UnsafeName "()")
1428+
1429+
Common.Bytes ->
1430+
simple (Common.UnsafeName "Bytes")
1431+
1432+
Common.Unit ->
1433+
simple (Common.UnsafeName "()")
1434+
1435+
Common.OneOf oneOfName _ ->
1436+
simple (Common.UnsafeName oneOfName)
1437+
1438+
Common.Value ->
1439+
simple (Common.UnsafeName "JsonEncodeValue")
1440+
1441+
Common.Ref ref ->
1442+
Common.unwrapRef ref
1443+
|> Tuple.second
1444+
|> simple
1445+
1446+
Common.Enum variants ->
1447+
CliMonad.enumName (NonEmpty.toList variants)
1448+
|> CliMonad.andThen
1449+
(\maybeName ->
1450+
maybeName
1451+
|> Maybe.withDefault (Common.UnsafeName "String")
1452+
|> simple
1453+
)
1454+
1455+
Common.Basic basicType { format } ->
1456+
CliMonad.withFormat basicType format (\{ annotation } -> Just annotation) Nothing
1457+
|> CliMonad.andThen
1458+
(\maybeAnnotation ->
1459+
case maybeAnnotation of
1460+
Just ann ->
1461+
fromAnnotation ann
1462+
1463+
Nothing ->
1464+
case basicType of
1465+
Common.String ->
1466+
simple (Common.UnsafeName "String")
1467+
1468+
Common.Integer ->
1469+
simple (Common.UnsafeName "Int")
1470+
1471+
Common.Boolean ->
1472+
simple (Common.UnsafeName "Bool")
1473+
1474+
Common.Number ->
1475+
simple (Common.UnsafeName "Float")
1476+
)
13821477

13831478

13841479
oneOfType :
@@ -1411,7 +1506,7 @@ oneOfType types =
14111506
readableName : String
14121507
readableName =
14131508
names
1414-
|> List.map fixOneOfName
1509+
|> List.map Common.toTypeName
14151510
|> String.join "_Or_"
14161511
in
14171512
{ type_ =
@@ -1621,20 +1716,7 @@ oneOfDeclaration ( oneOfName, variants ) =
16211716

16221717
toVariantName : Common.TypeName -> Common.UnsafeName -> String
16231718
toVariantName oneOfName variantName =
1624-
oneOfName ++ "__" ++ fixOneOfName variantName
1625-
1626-
1627-
{-| When we go from `Elm.Annotation` to `String` it includes the module name if it's an imported type.
1628-
We don't want that for our generated types, so we remove it here.
1629-
-}
1630-
fixOneOfName : Common.UnsafeName -> String
1631-
fixOneOfName name =
1632-
name
1633-
|> Common.unwrapUnsafe
1634-
|> String.replace "OpenApi.Nullable" "Nullable"
1635-
|> String.replace "." ""
1636-
|> Common.UnsafeName
1637-
|> Common.toTypeName
1719+
oneOfName ++ "__" ++ Common.toTypeName variantName
16381720

16391721

16401722
{-| Transform an OpenAPI type into an Elm annotation. Nullable values are represented using Nullable.

0 commit comments

Comments
 (0)