on master help sign in
Allow aliases in keys #49
commitf8ee19537495c509fa4cfd193c67a32e9f4c8a89 authorMichael Snoyman <michael@snoyman.com> dateMon 13 Oct 2014, 10:57 (12 years ago) parentf423d46
2 files changed+18
Data/Yaml.hs+7 test/main.hs+11
Data/Yaml.hs+7
@@ -182,6 +182,7 @@ data ParseException = NonScalarKey
182182 | InvalidYaml (Maybe YamlException)
183183 | AesonException String
184184 | OtherParseException SomeException
185+ | NonStringKeyAlias Y.AnchorName Value
185186 deriving (Show, Typeable)
186187instance Exception ParseException
187188
@@ -302,6 +303,12 @@ parseM a front = do
302303 CL.drop 1
303304 s <- case me of
304305 Just (EventScalar v tag style a') -> parseScalar v a' style tag
306+ Just (EventAlias an) -> do
307+ m <- lift get
308+ case Map.lookup an m of
309+ Nothing -> liftIO $ throwIO $ UnknownAlias an
310+ Just (String t) -> return t
311+ Just v -> liftIO $ throwIO $ NonStringKeyAlias an v
305312 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
306313 o <- parseO
307314
test/main.hs+11
@@ -127,6 +127,8 @@ main = hspec $ do
127127 in D.decode (D.encode v) `shouldBe` Just v
128128 it "no tags" $ D.encode (object ["word" .= ("true" :: String)]) `shouldBe` "word: 'true'\n"
129129
130+ it "aliases in keys #49" caseIssue49
131+
130132
131133specialStrings :: [T.Text]
132134specialStrings =
@@ -425,3 +427,12 @@ caseStripAlias =
425427 ])
426428 where
427429 src = "Default: &def\n foo: 1\n bar: 2\nObj:\n <<: *def\n key: 3\n"
430+
431+caseIssue49 :: Assertion
432+caseIssue49 =
433+ D.decodeEither src @?= Right (object
434+ [ "a" .= object [ "value" .= (1.0 :: Double) ]
435+ , "b" .= object [ "value" .= (1.2 :: Double) ]
436+ ])
437+ where
438+ src = "---\na:\n &id5 value: 1.0\nb:\n *id5: 1.2"