From 41fb46492831fc3fbeaefe5a4ff613d1b9757b3a Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 10 Apr 2023 08:44:51 +0300 Subject: [PATCH] fix: escape shorthand tags if needed --- emitterc.go | 12 +++++++----- encode_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/emitterc.go b/emitterc.go index f061b3d..354f5c6 100644 --- a/emitterc.go +++ b/emitterc.go @@ -420,7 +420,7 @@ func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) { return false } - if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) { + if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true, true) { return false } if !yaml_emitter_write_indent(emitter) { @@ -1080,7 +1080,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool { return false } if len(emitter.tag_data.suffix) > 0 { - if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, need_brackets, false) { return false } } @@ -1094,7 +1094,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool { if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) { return false } - if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false, false) { return false } if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) { @@ -1562,7 +1562,7 @@ func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool { return true } -func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool { +func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, allow_uri, need_whitespace bool) bool { if need_whitespace && !emitter.whitespace { if !put(emitter, ' ') { return false @@ -1571,8 +1571,10 @@ func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_ for i := 0; i < len(value); { var must_write bool switch value[i] { - case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']': + case ';', '/', '?', ':', '@', '&', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')': must_write = true + case '{', '}', '[', ']', ',': + must_write = allow_uri default: must_write = is_alpha(value, i) } diff --git a/encode_test.go b/encode_test.go index f355f5f..1dce883 100644 --- a/encode_test.go +++ b/encode_test.go @@ -717,6 +717,21 @@ var marshalTests = []struct { }, "!\n", }, + // https://github.com/go-faster/yaml/issues/50 + { + yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "[", + }, + "!<%5B>\n", + }, + { + yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "]", + }, + "!<%5D>\n", + }, // Enforced tagging with shorthand notation (issue #616). {