File tree 3 files changed +40
-5
lines changed
3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -3,13 +3,17 @@ module Filter
3
3
class Yaml < Base
4
4
class << self
5
5
def config
6
- @config ||= Struct . new ( :no_doc , :boolean_mode ) . new . tap do |config |
6
+ @config ||= Struct . new ( :no_doc , :boolean_mode , :raw ) . new . tap do |config |
7
7
config . no_doc = true
8
8
config . boolean_mode = true
9
+ config . raw = false
9
10
end
10
11
end
11
12
12
13
def configure ( opt )
14
+ opt . on ( '-r' , '--raw-string' , 'output raw strings' ) do
15
+ config . raw = true
16
+ end
13
17
opt . on ( '--disable-boolean-mode' , 'consider true/false/null as yaml literal' ) do
14
18
config . boolean_mode = false
15
19
end
@@ -26,6 +30,10 @@ def boolean_mode?
26
30
config . boolean_mode
27
31
end
28
32
33
+ def raw?
34
+ config . raw
35
+ end
36
+
29
37
def format ( val , record )
30
38
return unless yaml_obj = obj_to_yaml ( val , record )
31
39
@@ -36,6 +44,12 @@ def format(val, record)
36
44
37
45
def obj_to_yaml ( val , record )
38
46
case val
47
+ when String
48
+ if raw?
49
+ val
50
+ else
51
+ val . to_yaml ( colorize :)
52
+ end
39
53
when MatchData
40
54
record . to_yaml ( colorize :)
41
55
when Regexp
Original file line number Diff line number Diff line change 14
14
context 'with -r option' do
15
15
describe 'Output string' do
16
16
let ( :input ) { load_fixture ( 'yaml/multibyte_string.yml' ) }
17
- let ( :output ) { '🍣🍣🍣' }
17
+ let ( :args ) { '-t yaml -r _' }
18
+ let ( :expect_output ) { "🍣🍣🍣\n " }
18
19
19
- before { run_rf ( '-t yaml -r _' , input ) }
20
+ it_behaves_like 'a successful exec'
21
+ end
20
22
21
- it { expect ( last_command_started ) . to be_successfully_executed }
22
- it { expect ( last_command_started ) . to have_output output_string_eq output }
23
+ describe 'Ouput yaml to JSON string' do
24
+ let ( :input ) { load_fixture ( 'json/hash.json' ) }
25
+ let ( :args ) { '-t yaml -r "_.to_json(pretty_print: true)"' }
26
+ let ( :expect_output ) do
27
+ <<~OUTPUT
28
+ {
29
+ "foo": 1,
30
+ "bar": {
31
+ "baz": [
32
+ "a",
33
+ "b",
34
+ "c"
35
+ ]
36
+ },
37
+ "foo bar": "foo bar"
38
+ }
39
+ OUTPUT
40
+ end
41
+
42
+ it_behaves_like 'a successful exec'
23
43
end
24
44
end
25
45
Original file line number Diff line number Diff line change 31
31
-m, --minify minify json output
32
32
33
33
yaml options:
34
+ -r, --raw-string output raw strings
34
35
--disable-boolean-mode consider true/false/null as yaml literal
35
36
--[no-]doc [no] output document sperator (refers to ---) (default:--no-doc)
36
37
TEXT
You can’t perform that action at this time.
0 commit comments