Skip to content

Commit 9699207

Browse files
committed
Merge pull request Kong#63 from montanaflynn/master
Add option to include boilerplate
2 parents 8a6aba0 + 871bdce commit 9699207

2 files changed

Lines changed: 80 additions & 36 deletions

File tree

src/targets/go/native.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,106 +19,113 @@ module.exports = function (source, options) {
1919

2020
// Define Options
2121
var opts = util._extend({
22+
showBoilerplate: true,
2223
checkErrors: false,
2324
printBody: true,
2425
timeout: -1
2526
}, options)
2627

2728
var errorPlaceholder = opts.checkErrors ? 'err' : '_'
2829

30+
var indent = opts.showBoilerplate ? 1 : 0
31+
2932
var errorCheck = function () {
3033
if (opts.checkErrors) {
31-
code.push(1, 'if err != nil {')
32-
.push(2, 'panic(err)')
33-
.push(1, '}')
34+
code.push(indent, 'if err != nil {')
35+
.push(indent + 1, 'panic(err)')
36+
.push(indent, '}')
3437
}
3538
}
3639

3740
// Create boilerplate
38-
code.push('package main')
41+
if (opts.showBoilerplate) {
42+
code.push('package main')
3943
.blank()
4044
.push('import (')
41-
.push(1, '"fmt"')
45+
.push(indent, '"fmt"')
4246

43-
if (opts.timeout > 0) {
44-
code.push(1, '"time"')
45-
}
47+
if (opts.timeout > 0) {
48+
code.push(indent, '"time"')
49+
}
4650

47-
if (source.postData.text) {
48-
code.push(1, '"strings"')
49-
}
51+
if (source.postData.text) {
52+
code.push(indent, '"strings"')
53+
}
5054

51-
code.push(1, '"net/http"')
55+
code.push(indent, '"net/http"')
5256

53-
if (opts.printBody) {
54-
code.push(1, '"io/ioutil"')
55-
}
57+
if (opts.printBody) {
58+
code.push(indent, '"io/ioutil"')
59+
}
5660

57-
code.push(')')
61+
code.push(')')
5862
.blank()
5963
.push('func main() {')
6064
.blank()
65+
}
6166

6267
// Create client
6368
var client
6469
if (opts.timeout > 0) {
6570
client = 'client'
66-
code.push(1, 'client := http.Client{')
67-
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
68-
.push(1, '}')
69-
.blank()
71+
code.push(indent, 'client := http.Client{')
72+
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
73+
.push(indent, '}')
74+
.blank()
7075
} else {
7176
client = 'http.DefaultClient'
7277
}
7378

74-
code.push(1, 'url := "%s"', source.fullUrl)
75-
.blank()
79+
code.push(indent, 'url := "%s"', source.fullUrl)
80+
.blank()
7681

7782
// If we have body content or not create the var and reader or nil
7883
if (source.postData.text) {
79-
code.push(1, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
80-
.blank()
81-
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
82-
.blank()
84+
code.push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
85+
.blank()
86+
.push(indent, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
87+
.blank()
8388
} else {
84-
code.push(1, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
85-
.blank()
89+
code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
90+
.blank()
8691
}
8792

8893
errorCheck()
8994

9095
// Add headers
9196
if (Object.keys(source.allHeaders).length) {
9297
Object.keys(source.allHeaders).forEach(function (key) {
93-
code.push(1, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
98+
code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
9499
})
95100

96101
code.blank()
97102
}
98103

99104
// Make request
100-
code.push(1, 'res, %s := %s.Do(req)', errorPlaceholder, client)
105+
code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client)
101106
errorCheck()
102107

103108
// Get Body
104109
if (opts.printBody) {
105110
code.blank()
106-
.push(1, 'defer res.Body.Close()')
107-
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
111+
.push(indent, 'defer res.Body.Close()')
112+
.push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
108113
errorCheck()
109114
}
110115

111116
// Print it
112117
code.blank()
113-
.push(1, 'fmt.Println(res)')
118+
.push(indent, 'fmt.Println(res)')
114119

115120
if (opts.printBody) {
116-
code.push(1, 'fmt.Println(string(body))')
121+
code.push(indent, 'fmt.Println(string(body))')
117122
}
118123

119124
// End main block
120-
code.blank()
125+
if (opts.showBoilerplate) {
126+
code.blank()
121127
.push('}')
128+
}
122129

123130
return code.join()
124131
}

test/tests/go/native.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/* global it */
2+
13
'use strict'
24

3-
module.exports = function (snippet, fixtures) {}
5+
require('should')
6+
7+
module.exports = function (HTTPSnippet, fixtures) {
8+
it('should support false boilerplate option', function () {
9+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
10+
showBoilerplate: false
11+
})
12+
13+
result.should.be.a.String
14+
result.should.eql('url := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\npayload := strings.NewReader(\"foo=bar\")\n\nreq, _ := http.NewRequest(\"POST\", url, payload)\n\nreq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\nreq.Header.Add(\"accept\", \"application/json\")\nreq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\nres, _ := http.DefaultClient.Do(req)\n\ndefer res.Body.Close()\nbody, _ := ioutil.ReadAll(res.Body)\n\nfmt.Println(res)\nfmt.Println(string(body))')
15+
})
16+
it('should support checkErrors option', function () {
17+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
18+
checkErrors: true
19+
})
20+
21+
result.should.be.a.String
22+
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"net/http\"\n\t\"io/ioutil\"\n)\n\nfunc main() {\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, err := http.NewRequest(\"POST\", url, payload)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tdefer res.Body.Close()\n\tbody, err := ioutil.ReadAll(res.Body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res)\n\tfmt.Println(string(body))\n\n}')
23+
})
24+
it('should support printBody option', function () {
25+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
26+
printBody: false
27+
})
28+
29+
result.should.be.a.String
30+
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"net/http\"\n)\n\nfunc main() {\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, _ := http.NewRequest(\"POST\", url, payload)\n\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, _ := http.DefaultClient.Do(req)\n\n\tfmt.Println(res)\n\n}')
31+
})
32+
it('should support timeout option', function () {
33+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
34+
timeout: 30
35+
})
36+
37+
result.should.be.a.String
38+
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\"strings\"\n\t\"net/http\"\n\t\"io/ioutil\"\n)\n\nfunc main() {\n\n\tclient := http.Client{\n\t\tTimeout: time.Duration(30 * time.Second),\n\t}\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, _ := http.NewRequest(\"POST\", url, payload)\n\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, _ := client.Do(req)\n\n\tdefer res.Body.Close()\n\tbody, _ := ioutil.ReadAll(res.Body)\n\n\tfmt.Println(res)\n\tfmt.Println(string(body))\n\n}')
39+
})
40+
}

0 commit comments

Comments
 (0)