Skip to content

Commit

Permalink
Merge pull request #229 from mbj/add/fn-cidr
Browse files Browse the repository at this point in the history
Add support for Fn::Cidr
  • Loading branch information
mbj authored Jul 3, 2024
2 parents 337d759 + 848b671 commit ba95ad2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
33 changes: 22 additions & 11 deletions src/Stratosphere/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,44 @@ mkFunc key args = JSON.object [(key, JSON.Array $ fromList args)]
-- @List<AWS::EC2::Subnet::Id>@ then, you can use @RefList "SubnetIds"@ to
-- reference it.
data ValueList a
= ValueList [Value a]
| RefList Text
= Cidr (Value Text) (Value Text) (Value Text)
| GetAZs (Value Text)
| ImportValueList (Value Text)
| RefList Text
| Split Text (Value a)
| GetAZs (Value Text)
| ValueList [Value a]
deriving (Show, Eq)

instance IsList (ValueList a) where
type Item (ValueList a) = Value a
fromList = ValueList

toList = \case
(ValueList xs) -> xs
-- This is obviously not meaningful, but the IsList instance is so useful
-- that I decided to allow it.
(RefList _) -> []
(Cidr _ _ _) -> []
(GetAZs _) -> []
(ImportValueList _) -> []
(RefList _) -> []
(Split _ _) -> []
(GetAZs _) -> []
(ValueList xs) -> xs

instance JSON.ToJSON a => JSON.ToJSON (ValueList a) where
toJSON = \case
(ValueList vals) -> JSON.toJSON vals
(RefList ref) -> refToJSON ref
(ImportValueList ref) -> importValueToJSON ref
(Split d s) -> mkFunc "Fn::Split" [JSON.toJSON d, JSON.toJSON s]
(GetAZs r) -> JSON.object [("Fn::GetAZs", JSON.toJSON r)]
(Cidr ipBlock count cidrBits) -> JSON.object [("Fn::Cidr", cidrArray ipBlock count cidrBits)]
(GetAZs r) -> JSON.object [("Fn::GetAZs", JSON.toJSON r)]
(ImportValueList ref) -> importValueToJSON ref
(RefList ref) -> refToJSON ref
(Split d s) -> mkFunc "Fn::Split" [JSON.toJSON d, JSON.toJSON s]
(ValueList vals) -> JSON.toJSON vals
where
cidrArray :: Value Text -> Value Text -> Value Text -> JSON.Value
cidrArray ipBlock count cidrBits
= JSON.Array
[ JSON.toJSON ipBlock
, JSON.toJSON count
, JSON.toJSON cidrBits
]

-- | Class used to create a 'Ref' from another type.
class ToRef a b where
Expand Down
3 changes: 1 addition & 2 deletions stack-9.6.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
resolver: lts-22.18
compiler: ghc-9.6.5
resolver: lts-22.27
flags:
stratosphere:
development: true
Expand Down
8 changes: 4 additions & 4 deletions stack-9.6.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
packages: []
snapshots:
- completed:
sha256: 9bebedd3de0195aa01fd55de7f6d4446667440297a7315e69cd72c2610af265f
size: 713338
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/18.yaml
original: lts-22.18
sha256: bc144ddf301a5c99f2cf51c7de50279ba144fd4486cb3c66f87ed761d6bbf6e9
size: 719131
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/27.yaml
original: lts-22.27
3 changes: 3 additions & 0 deletions test/Stratosphere/ValuesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec = do

it "ImportValue and ImportValueList produce the same JSON" $ do
JSON.toJSON (ImportValue "MyVal" :: Value Text) `shouldBe` JSON.toJSON (ImportValueList "MyVal" :: ValueList Text)

it "Cidr produces expected JSON" $ do
JSON.toJSON @(ValueList Text) (Cidr "192.168.0.0/24" "6" "5") `shouldBe` JSON.object [("Fn::Cidr", JSON.Array ["192.168.0.0/24", "6", "5"])]

0 comments on commit ba95ad2

Please sign in to comment.