on master help sign in
Allow aliases in keys #49
f8ee195Michael Snoyman
Data/Yaml.hs+7
@@ -182,6 +182,7 @@ data ParseException = NonScalarKey
182 | InvalidYaml (Maybe YamlException)
182 | InvalidYaml (Maybe YamlException)
183 | AesonException String
183 | AesonException String
184 | OtherParseException SomeException
184 | OtherParseException SomeException
185+ | NonStringKeyAlias Y.AnchorName Value
185 deriving (Show, Typeable)
186 deriving (Show, Typeable)
186instance Exception ParseException
187instance Exception ParseException
187
188
@@ -302,6 +303,12 @@ parseM a front = do
302 CL.drop 1
303 CL.drop 1
303 s <- case me of
304 s <- case me of
304 Just (EventScalar v tag style a') -> parseScalar v a' style tag
305 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
305 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
312 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
306 o <- parseO
313 o <- parseO
307
314
test/main.hs+11
@@ -127,6 +127,8 @@ main = hspec $ do
127 in D.decode (D.encode v) `shouldBe` Just v
127 in D.decode (D.encode v) `shouldBe` Just v
128 it "no tags" $ D.encode (object ["word" .= ("true" :: String)]) `shouldBe` "word: 'true'\n"
128 it "no tags" $ D.encode (object ["word" .= ("true" :: String)]) `shouldBe` "word: 'true'\n"
129
129
130+ it "aliases in keys #49" caseIssue49
131+
130
132
131specialStrings :: [T.Text]
133specialStrings :: [T.Text]
132specialStrings =
134specialStrings =
@@ -425,3 +427,12 @@ caseStripAlias =
425 ])
427 ])
426 where
428 where
427 src = "Default: &def\n foo: 1\n bar: 2\nObj:\n <<: *def\n key: 3\n"
429 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"