Skip to content

Commit ec25886

Browse files
author
Mike Farah
committed
Removed redundant code, updated doc w.r.t reading json files
1 parent c2000a4 commit ec25886

File tree

16 files changed

+114
-113
lines changed

16 files changed

+114
-113
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Available Commands:
3535
new yaml n [--script/-s script_file] a.b.c newValueForC
3636
3737
Flags:
38-
-J, --fromjson[=false]: input as json
3938
-h, --help[=false]: help for yaml
4039
-j, --tojson[=false]: output as json
4140
-t, --trim[=true]: trim yaml output

coverage.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
go test -coverprofile=coverage.out && go tool cover -html=coverage.out

docs/convert/index.html

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@
252252
<ul class="md-nav__list" data-md-scrollfix>
253253

254254
<li class="md-nav__item">
255-
<a href="#yaml2json" title="Yaml2json" class="md-nav__link">
256-
Yaml2json
255+
<a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
256+
Yaml to Json
257257
</a>
258258

259259
</li>
260260

261261
<li class="md-nav__item">
262-
<a href="#json2yaml" title="json2yaml" class="md-nav__link">
263-
json2yaml
262+
<a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
263+
Json to Yaml
264264
</a>
265265

266266
</li>
@@ -293,15 +293,15 @@
293293
<ul class="md-nav__list" data-md-scrollfix>
294294

295295
<li class="md-nav__item">
296-
<a href="#yaml2json" title="Yaml2json" class="md-nav__link">
297-
Yaml2json
296+
<a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
297+
Yaml to Json
298298
</a>
299299

300300
</li>
301301

302302
<li class="md-nav__item">
303-
<a href="#json2yaml" title="json2yaml" class="md-nav__link">
304-
json2yaml
303+
<a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
304+
Json to Yaml
305305
</a>
306306

307307
</li>
@@ -323,10 +323,39 @@
323323

324324
<h1>Convert</h1>
325325

326-
<h3 id="yaml2json">Yaml2json</h3>
326+
<h3 id="yaml-to-json">Yaml to Json</h3>
327327
<p>To convert output to json, use the --tojson (or -j) flag. This can be used with any command.</p>
328-
<h3 id="json2yaml">json2yaml</h3>
329-
<p>To read in json, use the --fromjson (or -J) flag. This can be used with any command.</p>
328+
<p>Given a sample.yaml file of:</p>
329+
<pre><code class="yaml">b:
330+
c: 2
331+
</code></pre>
332+
333+
<p>then</p>
334+
<pre><code class="bash">yaml r -j sample.yaml b.c
335+
</code></pre>
336+
337+
<p>will output</p>
338+
<pre><code class="json">{&quot;b&quot;:{&quot;c&quot;:2}}
339+
</code></pre>
340+
341+
<h3 id="json-to-yaml">Json to Yaml</h3>
342+
<p>To read in json, just pass in a json file instead of yaml, it will just work :)</p>
343+
<p>e.g given a json file</p>
344+
<pre><code class="json">{&quot;a&quot;:&quot;Easy! as one two three&quot;,&quot;b&quot;:{&quot;c&quot;:2,&quot;d&quot;:[3,4]}}
345+
</code></pre>
346+
347+
<p>then</p>
348+
<pre><code class="bash">yaml r sample.json
349+
</code></pre>
350+
351+
<p>will output</p>
352+
<pre><code class="yaml">a: Easy! as one two three
353+
b:
354+
c: 2
355+
d:
356+
- 3
357+
- 4
358+
</code></pre>
330359

331360

332361

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@
328328

329329

330330
<h1 id="yaml">yaml</h1>
331-
<p>yaml is a lightweight and flexible command-line YAML processor</p>
331+
<p>yaml is a lightweight and portable command-line YAML processor</p>
332332
<p>The aim of the project is to be the <a href="https://github.com/stedolan/jq">jq</a> or sed of yaml files.</p>
333333
<h3 id="download-latest-binary"><a href="https://github.com/mikefarah/yaml/releases/latest">download latest binary</a></h3>
334334
<h3 id="get-the-source">get the source</h3>

docs/mkdocs/search_index.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"docs": [
33
{
44
"location": "/",
5-
"text": "yaml\n\n\nyaml is a lightweight and flexible command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\ndownload latest binary\n\n\nget the source\n\n\ngo get github.com/mikefarah/yaml\n\n\n\n\n.zip\n or \ntar.gz\n\n\nView on GitHub",
5+
"text": "yaml\n\n\nyaml is a lightweight and portable command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\ndownload latest binary\n\n\nget the source\n\n\ngo get github.com/mikefarah/yaml\n\n\n\n\n.zip\n or \ntar.gz\n\n\nView on GitHub",
66
"title": "Install"
77
},
88
{
99
"location": "/#yaml",
10-
"text": "yaml is a lightweight and flexible command-line YAML processor The aim of the project is to be the jq or sed of yaml files.",
10+
"text": "yaml is a lightweight and portable command-line YAML processor The aim of the project is to be the jq or sed of yaml files.",
1111
"title": "yaml"
1212
},
1313
{
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"location": "/read/",
25-
"text": "yaml r \nyaml file\n \npath\n\n\n\n\n\nBasic\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam",
25+
"text": "yaml r \nyaml_file|json_file\n \npath\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nBasic\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam",
2626
"title": "Read"
2727
},
2828
{
@@ -57,7 +57,7 @@
5757
},
5858
{
5959
"location": "/write/",
60-
"text": "yaml w \nyaml file\n \npath\n \nnew value\n\n\n\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml",
60+
"text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml",
6161
"title": "Write/Update"
6262
},
6363
{
@@ -102,18 +102,18 @@
102102
},
103103
{
104104
"location": "/convert/",
105-
"text": "Yaml2json\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can be used with any command.\n\n\njson2yaml\n\n\nTo read in json, use the --fromjson (or -J) flag. This can be used with any command.",
105+
"text": "Yaml to Json\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can be used with any command.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\nb\n:{\nc\n:2}}\n\n\n\n\nJson to Yaml\n\n\nTo read in json, just pass in a json file instead of yaml, it will just work :)\n\n\ne.g given a json file\n\n\n{\na\n:\nEasy! as one two three\n,\nb\n:{\nc\n:2,\nd\n:[3,4]}}\n\n\n\n\nthen\n\n\nyaml r sample.json\n\n\n\n\nwill output\n\n\na: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4",
106106
"title": "Convert"
107107
},
108108
{
109-
"location": "/convert/#yaml2json",
110-
"text": "To convert output to json, use the --tojson (or -j) flag. This can be used with any command.",
111-
"title": "Yaml2json"
109+
"location": "/convert/#yaml-to-json",
110+
"text": "To convert output to json, use the --tojson (or -j) flag. This can be used with any command. Given a sample.yaml file of: b:\n c: 2 then yaml r -j sample.yaml b.c will output { b :{ c :2}}",
111+
"title": "Yaml to Json"
112112
},
113113
{
114-
"location": "/convert/#json2yaml",
115-
"text": "To read in json, use the --fromjson (or -J) flag. This can be used with any command.",
116-
"title": "json2yaml"
114+
"location": "/convert/#json-to-yaml",
115+
"text": "To read in json, just pass in a json file instead of yaml, it will just work :) e.g given a json file { a : Easy! as one two three , b :{ c :2, d :[3,4]}} then yaml r sample.json will output a: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4",
116+
"title": "Json to Yaml"
117117
}
118118
]
119119
}

docs/read/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,10 @@
379379

380380
<h1>Read</h1>
381381

382-
<pre><code>yaml r &lt;yaml file&gt; &lt;path&gt;
382+
<pre><code>yaml r &lt;yaml_file|json_file&gt; &lt;path&gt;
383383
</code></pre>
384384

385+
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
385386
<h3 id="basic">Basic</h3>
386387
<p>Given a sample.yaml file of:</p>
387388
<pre><code class="yaml">b:

docs/sitemap.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@
44

55
<url>
66
<loc>/</loc>
7-
<lastmod>2017-04-13</lastmod>
7+
<lastmod>2017-04-19</lastmod>
88
<changefreq>daily</changefreq>
99
</url>
1010

1111

1212

1313
<url>
1414
<loc>/read/</loc>
15-
<lastmod>2017-04-13</lastmod>
15+
<lastmod>2017-04-19</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818

1919

2020

2121
<url>
2222
<loc>/write/</loc>
23-
<lastmod>2017-04-13</lastmod>
23+
<lastmod>2017-04-19</lastmod>
2424
<changefreq>daily</changefreq>
2525
</url>
2626

2727

2828

2929
<url>
3030
<loc>/create/</loc>
31-
<lastmod>2017-04-13</lastmod>
31+
<lastmod>2017-04-19</lastmod>
3232
<changefreq>daily</changefreq>
3333
</url>
3434

3535

3636

3737
<url>
3838
<loc>/convert/</loc>
39-
<lastmod>2017-04-13</lastmod>
39+
<lastmod>2017-04-19</lastmod>
4040
<changefreq>daily</changefreq>
4141
</url>
4242

docs/write/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@
365365

366366
<h1>Write/Update</h1>
367367

368-
<pre><code>yaml w &lt;yaml file&gt; &lt;path&gt; &lt;new value&gt;
368+
<pre><code>yaml w &lt;yaml_file|json_file&gt; &lt;path&gt; &lt;new value&gt;
369369
</code></pre>
370370

371+
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
371372
<h3 id="to-stdout">To Stdout</h3>
372373
<p>Given a sample.yaml file of:</p>
373374
<pre><code class="yaml">b:

json_converter.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ import (
55
"github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2"
66
)
77

8-
func fromJSONBytes(jsonBytes []byte, parsedData *map[interface{}]interface{}) {
9-
*parsedData = make(map[interface{}]interface{})
10-
var jsonData map[string]interface{}
11-
err := json.Unmarshal(jsonBytes, &jsonData)
12-
if err != nil {
13-
die("error parsing data: ", err)
14-
}
15-
16-
for key, value := range jsonData {
17-
(*parsedData)[key] = fromJSON(value)
18-
}
19-
}
20-
218
func jsonToString(context interface{}) string {
229
out, err := json.Marshal(toJSON(context))
2310
if err != nil {
@@ -26,27 +13,6 @@ func jsonToString(context interface{}) string {
2613
return string(out)
2714
}
2815

29-
func fromJSON(context interface{}) interface{} {
30-
switch context.(type) {
31-
case []interface{}:
32-
oldArray := context.([]interface{})
33-
newArray := make([]interface{}, len(oldArray))
34-
for index, value := range oldArray {
35-
newArray[index] = fromJSON(value)
36-
}
37-
return newArray
38-
case map[string]interface{}:
39-
oldMap := context.(map[string]interface{})
40-
newMap := make(map[interface{}]interface{})
41-
for key, value := range oldMap {
42-
newMap[key] = fromJSON(value)
43-
}
44-
return newMap
45-
default:
46-
return context
47-
}
48-
}
49-
5016
func toJSON(context interface{}) interface{} {
5117
switch context.(type) {
5218
case []interface{}:

json_converter_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
"os"
74
"testing"
85
)
96

10-
func TestJsonFromString(t *testing.T) {
11-
var data = parseJSONData(`
12-
{
13-
"b": {
14-
"c": 2
15-
}
16-
}
17-
`)
18-
assertResult(t, "map[b:map[c:2]]", fmt.Sprintf("%v", data))
19-
}
20-
21-
func TestJsonFromString_withArray(t *testing.T) {
22-
var data = parseJSONData(`
23-
{
24-
"b": [
25-
{ "c": 5 },
26-
{ "c": 6 }
27-
]
28-
}
29-
`)
30-
assertResult(t, "map[b:[map[c:5] map[c:6]]]", fmt.Sprintf("%v", data))
31-
}
32-
337
func TestJsonToString(t *testing.T) {
348
var data = parseData(`
359
---
@@ -48,13 +22,3 @@ b:
4822
`)
4923
assertResult(t, "{\"b\":[{\"item\":\"one\"},{\"item\":\"two\"}]}", jsonToString(data))
5024
}
51-
52-
func parseJSONData(rawData string) map[string]interface{} {
53-
var parsedData map[string]interface{}
54-
err := json.Unmarshal([]byte(rawData), &parsedData)
55-
if err != nil {
56-
fmt.Println("Error parsing json: ", err)
57-
os.Exit(1)
58-
}
59-
return parsedData
60-
}

0 commit comments

Comments
 (0)