on master help sign in
Updated as per review comments
commitb8b5756d0d4c86e187c2a4903f3333f8cf0d1980 authorgmorpheme <github@gmorpheme.net> dateMon 11 Feb 2019, 21:05 (8 years ago) parent39042f3
2 files changed+66−83
libyaml/src/Text/Libyaml.hs+45−54 yaml/test/Data/YamlSpec.hs+21−29
libyaml/src/Text/Libyaml.hs+45−54
@@ -32,11 +32,10 @@ module Text.Libyaml
3232 , FormatOptions
3333 , defaultFormatOptions
3434 , setWidth
35− , setCollectionTagRendering
36− , setPlainTagRendering
37− , setQuotedTagRendering
38− , renderNone
39− , renderAll
35+ , setTagRendering
36+ , renderScalarTags
37+ , renderAllTags
38+ , renderNoTags
4039 , renderUriTags
4140 -- * Error handling
4241 , YamlException (..)
@@ -127,6 +126,11 @@ data Tag = StrTag
127126 | NoTag
128127 deriving (Show, Eq, Read, Data, Typeable)
129128
129+tagSuppressed :: Tag -> Bool
130+tagSuppressed (NoTag) = True
131+tagSuppressed (UriTag "") = True
132+tagSuppressed _ = False
133+
130134type AnchorName = String
131135type Anchor = Maybe AnchorName
132136
@@ -471,8 +475,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
471475 len' = fromIntegral len :: CInt
472476 let thetag' = tagToString thetag
473477 withCString thetag' $ \tag' -> do
474− let pi0 = implicitPlain e
475− qi = implicitQuoted e
478+ let pi0 = tagsImplicit e
476479 (pi, style) =
477480 case style0 of
478481 PlainNoTag -> (1, Plain)
@@ -488,7 +491,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
488491 value' -- value
489492 len' -- length
490493 pi -- plain_implicit
491− qi -- quoted_implicit
494+ pi -- quoted_implicit
492495 style' -- style
493496 Just anchor' ->
494497 withCString anchor' $ \anchorP' -> do
@@ -500,7 +503,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
500503 value' -- value
501504 len' -- length
502505 0 -- plain_implicit
503− qi -- quoted_implicit
506+ pi -- quoted_implicit
504507 style' -- style
505508 EventSequenceStart tag style Nothing ->
506509 withCString (tagToString tag) $ \tag' -> do
@@ -509,7 +512,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
509512 er
510513 nullPtr
511514 tagP
512− (implicitColl e)
515+ (tagsImplicit e)
513516 (toEnum $ fromEnum style)
514517 EventSequenceStart tag style (Just anchor) ->
515518 withCString (tagToString tag) $ \tag' -> do
@@ -520,7 +523,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
520523 er
521524 anchorP
522525 tagP
523− (implicitColl e)
526+ (tagsImplicit e)
524527 (toEnum $ fromEnum style)
525528 EventSequenceEnd ->
526529 c_yaml_sequence_end_event_initialize er
@@ -531,7 +534,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
531534 er
532535 nullPtr
533536 tagP
534− (implicitColl e)
537+ (tagsImplicit e)
535538 (toEnum $ fromEnum style)
536539 EventMappingStart tag style (Just anchor) ->
537540 withCString (tagToString tag) $ \tag' -> do
@@ -542,7 +545,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
542545 er
543546 anchorP
544547 tagP
545− (implicitColl e)
548+ (tagsImplicit e)
546549 (toEnum $ fromEnum style)
547550 EventMappingEnd ->
548551 c_yaml_mapping_end_event_initialize er
@@ -555,17 +558,10 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
555558 unless (ret == 1) $ throwIO $ ToEventRawException ret
556559 f er
557560 where
558− implicitColl (EventMappingStart NoTag _ _) = 1
559− implicitColl (EventSequenceStart NoTag _ _) = 1
560− implicitColl (EventMappingStart (UriTag "") _ _) = 1
561− implicitColl (EventSequenceStart (UriTag "") _ _) = 1
562− implicitColl evt = toImplicitParam $ formatOptionsRenderCollectionTags opts evt
563− implicitPlain (EventScalar _ NoTag _ _) = 1
564− implicitPlain (EventScalar _ (UriTag "") _ _) = 1
565− implicitPlain evt = toImplicitParam $ formatOptionsRenderPlainScalarTags opts evt
566− implicitQuoted (EventScalar _ NoTag _ _) = 1
567− implicitQuoted (EventScalar _ (UriTag "") _ _) = 1
568− implicitQuoted evt = toImplicitParam $ formatOptionsRenderQuotedScalarTags opts evt
561+ tagsImplicit (EventScalar _ t _ _) | tagSuppressed t = 1
562+ tagsImplicit (EventMappingStart t _ _) | tagSuppressed t = 1
563+ tagsImplicit (EventSequenceStart t _ _) | tagSuppressed t = 1
564+ tagsImplicit evt = toImplicitParam $ formatOptionsRenderTags opts evt
569565
570566newtype ToEventRawException = ToEventRawException CInt
571567 deriving (Show, Typeable)
@@ -691,21 +687,32 @@ data TagRender = Explicit | Implicit
691687
692688toImplicitParam :: TagRender -> CInt
693689toImplicitParam Explicit = 0
694−toImplicitParam _ = 1
690+toImplicitParam Implicit = 1
695691
696−-- | A value for 'formatOptionsRenderCollectionTags' that renders no
697−-- tags.
692+-- | A value for 'formatOptionsRenderTags' that renders no
693+-- collection tags but all scalar tags (unless suppressed with styles
694+-- 'NoTag or 'PlainNoTag').
695+--
696+-- @since 0.11.1.0
697+renderScalarTags :: Event -> TagRender
698+renderScalarTags (EventScalar _ _ _ _) = Explicit
699+renderScalarTags (EventSequenceStart _ _ _) = Implicit
700+renderScalarTags (EventMappingStart _ _ _) = Implicit
701+renderScalarTags _ = Implicit
702+
703+-- | A value for 'formatOptionsRenderTags' that renders all
704+-- tags (except 'NoTag' tag and 'PlainNoTag' style).
698705--
699706-- @since 0.11.1.0
700−renderNone :: Event -> TagRender
701−renderNone _ = Implicit
707+renderAllTags :: Event -> TagRender
708+renderAllTags _ = Explicit
702709
703−-- | A value for 'formatOptionsRenderCollectionTags' that renders all
704−-- tags (except 'NoTag' and 'PlainNoTag' for flow scalars).
710+-- | A value for 'formatOptionsRenderTags' that renders no
711+-- tags.
705712--
706713-- @since 0.11.1.0
707−renderAll :: Event -> TagRender
708−renderAll _ = Explicit
714+renderNoTags :: Event -> TagRender
715+renderNoTags _ = Implicit
709716
710717-- | A value for 'formatOptionsRenderCollectionTags' that renders tags
711718-- which are instances of 'UriTag'
@@ -722,9 +729,7 @@ renderUriTags _ = Implicit
722729-- @since 0.10.2.0
723730data FormatOptions = FormatOptions
724731 { formatOptionsWidth :: Maybe Int
725− , formatOptionsRenderCollectionTags :: Event -> TagRender
726− , formatOptionsRenderPlainScalarTags :: Event -> TagRender
727− , formatOptionsRenderQuotedScalarTags :: Event -> TagRender
732+ , formatOptionsRenderTags :: Event -> TagRender
728733 }
729734
730735-- |
@@ -732,9 +737,7 @@ data FormatOptions = FormatOptions
732737defaultFormatOptions :: FormatOptions
733738defaultFormatOptions = FormatOptions
734739 { formatOptionsWidth = Just 80 -- by default the width is set to 0 in the C code, which gets turned into 80 in yaml_emitter_emit_stream_start
735− , formatOptionsRenderCollectionTags = renderNone
736− , formatOptionsRenderPlainScalarTags = renderAll
737− , formatOptionsRenderQuotedScalarTags = renderAll
740+ , formatOptionsRenderTags = renderScalarTags
738741 }
739742
740743-- | Set the maximum number of columns in the YAML output, or 'Nothing' for infinite. By default, the limit is 80 characters.
@@ -743,23 +746,11 @@ defaultFormatOptions = FormatOptions
743746setWidth :: Maybe Int -> FormatOptions -> FormatOptions
744747setWidth w opts = opts { formatOptionsWidth = w }
745748
746−-- | Control when and whether tags on collections are rendered to output.
747−--
748−-- @since 0.11.1.0
749−setCollectionTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
750−setCollectionTagRendering f opts = opts { formatOptionsRenderCollectionTags = f }
751−
752−-- | Control when and whether tags on plain scalars are rendered to output.
753−--
754−-- @since 0.11.1.0
755−setPlainTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
756−setPlainTagRendering f opts = opts { formatOptionsRenderPlainScalarTags = f }
757−
758−-- | Control when and whether tags on quoted scalars are rendered to output.
749+-- | Control when and whether tags are rendered to output.
759750--
760751-- @since 0.11.1.0
761−setQuotedTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
762−setQuotedTagRendering f opts = opts { formatOptionsRenderQuotedScalarTags = f }
752+setTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
753+setTagRendering f opts = opts { formatOptionsRenderTags = f }
763754
764755encode :: MonadResource m => ConduitM Event o m ByteString
765756encode = encodeWith defaultFormatOptions
yaml/test/Data/YamlSpec.hs+21−29
@@ -213,7 +213,7 @@ spec = do
213213 go "12.3015e+02" (1230.15 :: Scientific)
214214 go "1230.15" (1230.15 :: Scientific)
215215
216− describe "Text.Libyaml with collection tags off" $ do
216+ describe "Text.Libyaml with default tag rendering" $ do
217217 let enc = testEncodeWith Y.defaultFormatOptions
218218 it "elides custom sequence tags" $
219219 enc taggedSequence `shouldReturn` "[]\n"
@@ -231,8 +231,16 @@ spec = do
231231 enc mixedTagSampleA `shouldReturn` "- {}\n"
232232 it "in combination of tags, anchors and styles, outputs only the scalar tags" $
233233 enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c [&d !!null '']\n"
234− describe "Text.Libyaml with collection tags on" $ do
235− let enc = testEncodeWith $ Y.setCollectionTagRendering Y.renderAll Y.defaultFormatOptions
234+ it "outputs tags when double quoted" $
235+ enc [Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing] `shouldReturn` "!!str \"foo\"\n"
236+ it "outputs tags when single quoted" $
237+ enc [Y.EventScalar "foo" Y.StrTag Y.SingleQuoted Nothing] `shouldReturn` "!!str 'foo'\n"
238+ it "outputs tags on literal text" $
239+ enc [Y.EventScalar "foo" Y.StrTag Y.Literal Nothing] `shouldReturn` "!!str |-\n foo\n"
240+ it "outputs tags on folded text" $
241+ enc [Y.EventScalar "foo" Y.StrTag Y.Folded Nothing] `shouldReturn` "!!str >-\n foo\n"
242+ describe "Text.Libyaml with all tags on" $ do
243+ let enc = testEncodeWith $ Y.setTagRendering Y.renderAllTags Y.defaultFormatOptions
236244 it "will output custom sequence tags" $
237245 enc taggedSequence `shouldReturn` "!foo []\n"
238246 it "will output custom mapping tags" $
@@ -249,8 +257,12 @@ spec = do
249257 enc mixedTagSampleA `shouldReturn` "- !foo {}\n"
250258 it "in combination of tags, anchors and styles, outputs all the tags" $
251259 enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
252− describe "Text.Libyaml with collection uri tags on" $ do
253− let enc = testEncodeWith $ Y.setCollectionTagRendering Y.renderUriTags Y.defaultFormatOptions
260+ it "outputs plain tags" $
261+ enc [Y.EventScalar "foo" Y.StrTag Y.Plain Nothing] `shouldReturn` "!!str foo\n"
262+ it "respects PlainNoTag tags" $
263+ enc [Y.EventScalar "foo" Y.StrTag Y.PlainNoTag Nothing] `shouldReturn` "foo\n"
264+ describe "Text.Libyaml with uri tags on" $ do
265+ let enc = testEncodeWith $ Y.setTagRendering Y.renderUriTags Y.defaultFormatOptions
254266 it "will output custom sequence tags" $
255267 enc taggedSequence `shouldReturn` "!foo []\n"
256268 it "will output custom mapping tags" $
@@ -266,31 +278,13 @@ spec = do
266278 it "handles mixed tag usages outputting all mapping and sequence tags" $
267279 enc mixedTagSampleA `shouldReturn` "- !foo {}\n"
268280 it "in combination of tags, anchors and styles, outputs all the tags" $
269− enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
270− describe "Text.Libyaml with plain scalar tags on" $ do
271− let enc = testEncodeWith $ Y.setPlainTagRendering Y.renderAll Y.defaultFormatOptions
272− it "outputs plain tags" $
273− enc [Y.EventScalar "foo" Y.StrTag Y.Plain Nothing] `shouldReturn` "!!str foo\n"
274− it "respects PlainNoTag tags" $
275− enc [Y.EventScalar "foo" Y.StrTag Y.PlainNoTag Nothing] `shouldReturn` "foo\n"
276− describe "Text.Libyaml with plain scalar tags off" $ do
277− let enc = testEncodeWith $ Y.setPlainTagRendering Y.renderNone Y.defaultFormatOptions
281+ enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c !baz [&d '']\n"
282+ describe "Text.Libyaml with tags off" $ do
283+ let enc = testEncodeWith $ Y.setTagRendering Y.renderNoTags Y.defaultFormatOptions
278284 it "outputs plain tags" $
279285 enc [Y.EventScalar "foo" Y.StrTag Y.Plain Nothing] `shouldReturn` "foo\n"
280286 it "respects PlainNoTag tags" $
281287 enc [Y.EventScalar "foo" Y.StrTag Y.PlainNoTag Nothing] `shouldReturn` "foo\n"
282− describe "Text.Libyaml with quoted scalar tags on" $ do
283− let enc = testEncodeWith $ Y.setQuotedTagRendering Y.renderAll Y.defaultFormatOptions
284− it "outputs tags when double quoted" $
285− enc [Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing] `shouldReturn` "!!str \"foo\"\n"
286− it "outputs tags when single quoted" $
287− enc [Y.EventScalar "foo" Y.StrTag Y.SingleQuoted Nothing] `shouldReturn` "!!str 'foo'\n"
288− it "outputs tags on literal text" $
289− enc [Y.EventScalar "foo" Y.StrTag Y.Literal Nothing] `shouldReturn` "!!str |-\n foo\n"
290− it "outputs tags on folded text" $
291− enc [Y.EventScalar "foo" Y.StrTag Y.Folded Nothing] `shouldReturn` "!!str >-\n foo\n"
292− describe "Text.Libyaml with quoted scalar tags off" $ do
293− let enc = testEncodeWith $ Y.setQuotedTagRendering Y.renderNone Y.defaultFormatOptions
294288 it "elides tags when double quoted" $
295289 enc [Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing] `shouldReturn` "\"foo\"\n"
296290 it "elides tags when single quoted" $
@@ -302,9 +296,7 @@ spec = do
302296 describe "Text.Libyaml with only UriTags set to render " $ do
303297 let enc =
304298 testEncodeWith $
305− Y.setCollectionTagRendering Y.renderUriTags $
306− Y.setPlainTagRendering Y.renderUriTags $
307− Y.setQuotedTagRendering Y.renderUriTags Y.defaultFormatOptions
299+ Y.setTagRendering Y.renderUriTags $ Y.defaultFormatOptions
308300 it "outputs only UriTags" $
309301 enc
310302 [ Y.EventSequenceStart Y.NoTag Y.FlowSequence Nothing