Skip to content

Commit d4de601

Browse files
author
Montana Flynn
committed
Small refactor to make lines shorter
1 parent 4c3dbd1 commit d4de601

File tree

8 files changed

+31
-16
lines changed

8 files changed

+31
-16
lines changed

src/targets/go/native.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function (options) {
77
indent: ' '
88
}, options);
99

10-
// Set helper object for later
10+
// Set some shortcuts
1111
var req = {
1212
url: this.source.fullUrl,
1313
method: this.source.method,
@@ -17,15 +17,9 @@ module.exports = function (options) {
1717
headers: this.source.headersObj
1818
};
1919

20-
// Find out if we have body content or not
2120
var bodyPresent = this.source.postData && this.source.postData.text;
22-
if (bodyPresent) {
23-
req.body = 'strings.NewReader(' + JSON.stringify(this.source.postData.text) + ')';
24-
} else {
25-
req.body = "nil";
26-
}
2721

28-
// Start writing code out
22+
// Let's Go!
2923
var code = [];
3024

3125
// Create boilerplate
@@ -40,7 +34,18 @@ module.exports = function (options) {
4034

4135
// Create client
4236
code.push('\tclient := &http.Client{}');
43-
code.push('\treq, _ := http.NewRequest("' + req.method + '", "' + req.url + '", ' + req.body + ')');
37+
code.push('\turl := "' + req.url + '"')
38+
39+
// If we have body content or not create the var and reader or nil
40+
if (bodyPresent) {
41+
code.push('\tbody := ' + JSON.stringify(this.source.postData.text))
42+
req.body = 'strings.NewReader(body)'
43+
} else {
44+
req.body = 'nil'
45+
}
46+
47+
code.push('\treq, _ := http.NewRequest("' + req.method + '", url, ' + req.body + ')');
48+
4449

4550
// Add headers
4651
var headersPresent = this.source.headers && this.source.headers.length;

test/fixtures/output/go/native/application-form-encoded.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88

99
func main() {
1010
client := &http.Client{}
11-
req, _ := http.NewRequest("POST", "http://mockbin.com/har", strings.NewReader("foo=bar&hello=world"))
11+
url := "http://mockbin.com/har"
12+
body := "foo=bar&hello=world"
13+
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
1214
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1315
res, _ := client.Do(req)
1416
fmt.Printf("%+v", res)

test/fixtures/output/go/native/application-json.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88

99
func main() {
1010
client := &http.Client{}
11-
req, _ := http.NewRequest("POST", "http://mockbin.com/har", strings.NewReader("{\"foo\": \"bar\"}"))
11+
url := "http://mockbin.com/har"
12+
body := "{\"foo\": \"bar\"}"
13+
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
1214
req.Header.Add("Content-Type", "application/json")
1315
res, _ := client.Do(req)
1416
fmt.Printf("%+v", res)

test/fixtures/output/go/native/cookies.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
func main() {
99
client := &http.Client{}
10-
req, _ := http.NewRequest("POST", "http://mockbin.com/har", nil)
10+
url := "http://mockbin.com/har"
11+
req, _ := http.NewRequest("POST", url, nil)
1112
req.Header.Add("Cookie", "foo=bar; bar=baz")
1213
res, _ := client.Do(req)
1314
fmt.Printf("%+v", res)

test/fixtures/output/go/native/full.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88

99
func main() {
1010
client := &http.Client{}
11-
req, _ := http.NewRequest("POST", "http://mockbin.com/har?baz=abc&foo=bar&foo=baz", strings.NewReader("foo=bar"))
11+
url := "http://mockbin.com/har?baz=abc&foo=bar&foo=baz"
12+
body := "foo=bar"
13+
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
1214
req.Header.Add("Accept", "application/json")
1315
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1416
req.Header.Add("Cookie", "foo=bar; bar=baz")

test/fixtures/output/go/native/headers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
func main() {
99
client := &http.Client{}
10-
req, _ := http.NewRequest("GET", "http://mockbin.com/har", nil)
10+
url := "http://mockbin.com/har"
11+
req, _ := http.NewRequest("GET", url, nil)
1112
req.Header.Add("Accept", "application/json")
1213
req.Header.Add("X-Foo", "Bar")
1314
res, _ := client.Do(req)

test/fixtures/output/go/native/query.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
func main() {
99
client := &http.Client{}
10-
req, _ := http.NewRequest("GET", "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz", nil)
10+
url := "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz"
11+
req, _ := http.NewRequest("GET", url, nil)
1112
res, _ := client.Do(req)
1213
fmt.Printf("%+v", res)
1314
}

test/fixtures/output/go/native/short.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
func main() {
99
client := &http.Client{}
10-
req, _ := http.NewRequest("GET", "http://mockbin.com/har", nil)
10+
url := "http://mockbin.com/har"
11+
req, _ := http.NewRequest("GET", url, nil)
1112
res, _ := client.Do(req)
1213
fmt.Printf("%+v", res)
1314
}

0 commit comments

Comments
 (0)