1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: change all '$expr' to '${expr}' (#16428)

This commit is contained in:
yuyi
2022-11-15 21:53:13 +08:00
committed by GitHub
parent 56239b4a23
commit 017ace6ea7
859 changed files with 7156 additions and 7135 deletions

View File

@ -18,7 +18,7 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
req_headers := req.build_request_headers(method, host_name, path)
$if trace_http_request ? {
eprintln('> $req_headers')
eprintln('> ${req_headers}')
}
// println(req_headers)
ssl_conn.write_string(req_headers) or { return err }
@ -31,7 +31,7 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
readcounter++
len := ssl_conn.socket_read_into_ptr(bp, bufsize) or { break }
$if debug_http ? {
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
eprintln('ssl_do, read ${readcounter:4d} | len: ${len}')
eprintln('-'.repeat(20))
eprintln(unsafe { tos(bp, len) })
eprintln('-'.repeat(20))
@ -41,7 +41,7 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
ssl_conn.shutdown()!
response_text := content.str()
$if trace_http_response ? {
eprintln('< $response_text')
eprintln('< ${response_text}')
}
return parse_response(response_text)
}

View File

@ -20,13 +20,13 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
addr := host_name
sdata := req.build_request_headers(method, host_name, path)
$if trace_http_request ? {
eprintln('> $sdata')
eprintln('> ${sdata}')
}
length := C.request(&ctx, port, addr.to_wide(), sdata.str, &buff)
C.vschannel_cleanup(&ctx)
response_text := unsafe { buff.vstring_with_len(length) }
$if trace_http_response ? {
eprintln('< $response_text')
eprintln('< ${response_text}')
}
return parse_response(response_text)
}

View File

@ -143,7 +143,7 @@ pub fn (c &Cookie) str() string {
}
if c.expires.year > 1600 {
e := c.expires
time_str := '$e.weekday_str(), $e.day.str() $e.smonth() $e.year $e.hhmmss() GMT'
time_str := '${e.weekday_str()}, ${e.day.str()} ${e.smonth()} ${e.year} ${e.hhmmss()} GMT'
b.write_string('; expires=')
b.write_string(time_str)
}
@ -214,7 +214,7 @@ pub fn sanitize_cookie_value(v string) string {
}
// Check for the existence of a space or comma
if val.starts_with(' ') || val.ends_with(' ') || val.starts_with(',') || val.ends_with(',') {
return '"$v"'
return '"${v}"'
}
return v
}

View File

@ -9,14 +9,14 @@ import os
// and saves it in the output file path `out_file_path`.
pub fn download_file(url string, out_file_path string) ! {
$if debug_http ? {
println('http.download_file url=$url out_file_path=$out_file_path')
println('http.download_file url=${url} out_file_path=${out_file_path}')
}
s := get(url) or { return err }
if s.status() != .ok {
return error('received http code $s.status_code')
return error('received http code ${s.status_code}')
}
$if debug_http ? {
println('http.download_file saving $s.body.len bytes')
println('http.download_file saving ${s.body.len} bytes')
}
os.write_file(out_file_path, s.body)!
}

View File

@ -598,7 +598,7 @@ pub fn (h Header) join(other Header) Header {
for v in other.custom_values(k, exact: true) {
combined.add_custom(k, v) or {
// panic because this should never fail
panic('unexpected error: $err')
panic('unexpected error: ${err}')
}
}
}
@ -634,7 +634,7 @@ struct HeaderKeyError {
}
pub fn (err HeaderKeyError) msg() string {
return "Invalid header key: '$err.header'"
return "Invalid header key: '${err.header}'"
}
pub fn (err HeaderKeyError) code() int {

View File

@ -374,7 +374,7 @@ fn test_parse_headers() ? {
assert parse_headers('foo: bar\r\nfoo:baz')?.custom_values('foo') == ['bar', 'baz']
if x := parse_headers(' oops: oh no') {
return error('should have errored, but got $x')
return error('should have errored, but got ${x}')
}
}

View File

@ -93,7 +93,7 @@ pub mut:
pub fn post_multipart_form(url string, conf PostMultipartFormConfig) !Response {
body, boundary := multipart_form_body(conf.form, conf.files)
mut header := conf.header
header.set(.content_type, 'multipart/form-data; boundary="$boundary"')
header.set(.content_type, 'multipart/form-data; boundary="${boundary}"')
return fetch(
method: .post
url: url
@ -137,7 +137,7 @@ pub fn fetch(config FetchConfig) !Response {
if config.url == '' {
return error('http.fetch: empty url')
}
url := build_url_from_fetch(config) or { return error('http.fetch: invalid url $config.url') }
url := build_url_from_fetch(config) or { return error('http.fetch: invalid url ${config.url}') }
req := Request{
method: config.method
url: url
@ -170,7 +170,7 @@ pub fn url_encode_form_data(data map[string]string) string {
for key_, value_ in data {
key := urllib.query_escape(key_)
value := urllib.query_escape(value_)
pieces << '$key=$value'
pieces << '${key}=${value}'
}
return pieces.join('&')
}
@ -189,7 +189,7 @@ fn build_url_from_fetch(config FetchConfig) !string {
}
mut pieces := []string{cap: config.params.len}
for key, val in config.params {
pieces << '$key=$val'
pieces << '${key}=${val}'
}
mut query := pieces.join('&')
if url.raw_query.len > 1 {

View File

@ -14,12 +14,12 @@ fn test_http_get_from_vlang_utc_now() {
}
urls := ['http://vlang.io/utc_now', 'https://vlang.io/utc_now']
for url in urls {
println('Test getting current time from $url by http.get')
println('Test getting current time from ${url} by http.get')
res := http.get(url) or { panic(err) }
assert res.status() == .ok
assert res.body.len > 0
assert res.body.int() > 1566403696
println('Current time is: $res.body.int()')
println('Current time is: ${res.body.int()}')
}
}
@ -36,7 +36,7 @@ fn test_public_servers() {
// 'https://yahoo.com/robots.txt',
]
for url in urls {
println('Testing http.get on public url: $url ')
println('Testing http.get on public url: ${url} ')
res := http.get(url) or { panic(err) }
assert res.status() == .ok
assert res.body.len > 0

View File

@ -25,8 +25,8 @@ fn main() {
// FILE AUTOGENERATED BY `build.vsh` - DO NOT MANUALLY EDIT
const (
db = $mt_map
ext_to_mt_str = $ext_to_mt_str
db = ${mt_map}
ext_to_mt_str = ${ext_to_mt_str}
)
')!
execute('${@VEXE} fmt -w db.v')

View File

@ -21,7 +21,7 @@ pub fn get_mime_type(ext string) string {
pub fn get_content_type(mt string) string {
mt_struct := db[mt]
charset := if mt_struct.charset.len > 0 { mt_struct.charset.to_lower() } else { 'utf-8' }
return '$mt; charset=$charset'
return '${mt}; charset=${charset}'
}
// returns the default extension for the given MIME type

View File

@ -54,13 +54,13 @@ pub fn (mut req Request) add_custom_header(key string, val string) ! {
// do will send the HTTP request and returns `http.Response` as soon as the response is recevied
pub fn (req &Request) do() !Response {
mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url $req.url') }
mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url ${req.url}') }
mut rurl := url
mut resp := Response{}
mut no_redirects := 0
for {
if no_redirects == max_redirects {
return error('http.request.do: maximum number of redirects reached ($max_redirects)')
return error('http.request.do: maximum number of redirects reached (${max_redirects})')
}
qresp := req.method_and_url_to_response(req.method, rurl)!
resp = qresp
@ -75,12 +75,12 @@ pub fn (req &Request) do() !Response {
mut redirect_url := resp.header.get(.location) or { '' }
if redirect_url.len > 0 && redirect_url[0] == `/` {
url.set_path(redirect_url) or {
return error('http.request.do: invalid path in redirect: "$redirect_url"')
return error('http.request.do: invalid path in redirect: "${redirect_url}"')
}
redirect_url = url.str()
}
qrurl := urllib.parse(redirect_url) or {
return error('http.request.do: invalid URL in redirect "$redirect_url"')
return error('http.request.do: invalid URL in redirect "${redirect_url}"')
}
rurl = qrurl
no_redirects++
@ -92,7 +92,7 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) !Res
host_name := url.hostname()
scheme := url.scheme
p := url.escaped_path().trim_left('/')
path := if url.query().len > 0 { '/$p?$url.query().encode()' } else { '/$p' }
path := if url.query().len > 0 { '/${p}?${url.query().encode()}' } else { '/${p}' }
mut nport := url.port().int()
if nport == 0 {
if scheme == 'http' {
@ -109,34 +109,35 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) !Res
return res
} else if scheme == 'http' {
// println('http_do( $nport, $method, $host_name, $path )')
res := req.http_do('$host_name:$nport', method, path)!
res := req.http_do('${host_name}:${nport}', method, path)!
return res
}
return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"')
return error('http.request.method_and_url_to_response: unsupported scheme: "${scheme}"')
}
fn (req &Request) build_request_headers(method Method, host_name string, path string) string {
ua := req.user_agent
mut uheaders := []string{}
if !req.header.contains(.host) {
uheaders << 'Host: $host_name\r\n'
uheaders << 'Host: ${host_name}\r\n'
}
if !req.header.contains(.user_agent) {
uheaders << 'User-Agent: $ua\r\n'
uheaders << 'User-Agent: ${ua}\r\n'
}
if req.data.len > 0 && !req.header.contains(.content_length) {
uheaders << 'Content-Length: $req.data.len\r\n'
uheaders << 'Content-Length: ${req.data.len}\r\n'
}
for key in req.header.keys() {
if key == CommonHeader.cookie.str() {
continue
}
val := req.header.custom_values(key).join('; ')
uheaders << '$key: $val\r\n'
uheaders << '${key}: ${val}\r\n'
}
uheaders << req.build_request_cookies_header()
version := if req.version == .unknown { Version.v1_1 } else { req.version }
return '$method $path $version\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
return '${method} ${path} ${version}\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' +
req.data
}
fn (req &Request) build_request_cookies_header() string {
@ -145,7 +146,7 @@ fn (req &Request) build_request_cookies_header() string {
}
mut cookie := []string{}
for key, val in req.cookies {
cookie << '$key=$val'
cookie << '${key}=${val}'
}
cookie << req.header.values(.cookie)
return 'Cookie: ' + cookie.join('; ') + '\r\n'
@ -160,13 +161,13 @@ fn (req &Request) http_do(host string, method Method, path string) !Response {
// TODO this really needs to be exposed somehow
client.write(s.bytes())!
$if trace_http_request ? {
eprintln('> $s')
eprintln('> ${s}')
}
mut bytes := io.read_all(reader: client)!
client.close()!
response_text := bytes.bytestr()
$if trace_http_response ? {
eprintln('< $response_text')
eprintln('< ${response_text}')
}
return parse_response(response_text)
}
@ -286,7 +287,7 @@ pub struct UnexpectedExtraAttributeError {
}
pub fn (err UnexpectedExtraAttributeError) msg() string {
return 'Encountered unexpected extra attributes: $err.attributes'
return 'Encountered unexpected extra attributes: ${err.attributes}'
}
pub struct MultiplePathAttributesError {

View File

@ -35,7 +35,7 @@ fn test_parse_request_not_http() {
fn test_parse_request_no_headers() {
mut reader_ := reader('GET / HTTP/1.1\r\n\r\n')
req := parse_request(mut reader_) or { panic('did not parse: $err') }
req := parse_request(mut reader_) or { panic('did not parse: ${err}') }
assert req.method == .get
assert req.url == '/'
assert req.version == .v1_1
@ -43,27 +43,27 @@ fn test_parse_request_no_headers() {
fn test_parse_request_two_headers() {
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a\r\nTest2: B\r\n\r\n')
req := parse_request(mut reader_) or { panic('did not parse: $err') }
req := parse_request(mut reader_) or { panic('did not parse: ${err}') }
assert req.header.custom_values('Test1') == ['a']
assert req.header.custom_values('Test2') == ['B']
}
fn test_parse_request_two_header_values() {
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a; b\r\nTest2: c\r\nTest2: d\r\n\r\n')
req := parse_request(mut reader_) or { panic('did not parse: $err') }
req := parse_request(mut reader_) or { panic('did not parse: ${err}') }
assert req.header.custom_values('Test1') == ['a; b']
assert req.header.custom_values('Test2') == ['c', 'd']
}
fn test_parse_request_body() {
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a\r\nTest2: b\r\nContent-Length: 4\r\n\r\nbodyabc')
req := parse_request(mut reader_) or { panic('did not parse: $err') }
req := parse_request(mut reader_) or { panic('did not parse: ${err}') }
assert req.data == 'body'
}
fn test_parse_request_line() {
method, target, version := parse_request_line('GET /target HTTP/1.1') or {
panic('did not parse: $err')
panic('did not parse: ${err}')
}
assert method == .get
assert target.str() == '/target'
@ -127,16 +127,16 @@ fn test_parse_multipart_form() {
file := 'bar.v'
ct := 'application/octet-stream'
contents := ['baz', 'buzz']
data := "--$boundary
Content-Disposition: form-data; name=\"${names[0]}\"; filename=\"$file\"\r
Content-Type: $ct\r
data := "--${boundary}
Content-Disposition: form-data; name=\"${names[0]}\"; filename=\"${file}\"\r
Content-Type: ${ct}\r
\r
${contents[0]}\r
--$boundary\r
--${boundary}\r
Content-Disposition: form-data; name=\"${names[1]}\"\r
\r
${contents[1]}\r
--$boundary--\r
--${boundary}--\r
"
form, files := parse_multipart_form(data, boundary)
assert files == {
@ -176,7 +176,7 @@ fn test_multipart_form_body() {
fn test_parse_large_body() {
body := 'A'.repeat(101) // greater than max_bytes
req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body'
req := 'GET / HTTP/1.1\r\nContent-Length: ${body.len}\r\n\r\n${body}'
mut reader_ := reader(req)
result := parse_request(mut reader_)!
assert result.data.len == body.len

View File

@ -29,7 +29,7 @@ pub fn (resp Response) bytes() []u8 {
// Formats resp to a string suitable for HTTP response transmission
pub fn (resp Response) bytestr() string {
return 'HTTP/$resp.http_version $resp.status_code $resp.status_msg\r\n' + '${resp.header.render(
return 'HTTP/${resp.http_version} ${resp.status_code} ${resp.status_msg}\r\n' + '${resp.header.render(
version: resp.version()
)}\r\n' + resp.body
}
@ -98,7 +98,7 @@ pub fn (mut r Response) set_status(s Status) {
// version parses the version
pub fn (r Response) version() Version {
return version_from_str('HTTP/$r.http_version')
return version_from_str('HTTP/${r.http_version}')
}
// set_version sets the http_version string of the response
@ -108,7 +108,7 @@ pub fn (mut r Response) set_version(v Version) {
return
}
maj, min := v.protos()
r.http_version = '${maj}.$min'
r.http_version = '${maj}.${min}'
}
pub struct ResponseConfig {

View File

@ -30,7 +30,7 @@ fn check_headers(expected []string, found []string) ! {
assert expected.len == found.len
for header in expected {
if !found.contains(header) {
return error('expected header "$header" not in $found')
return error('expected header "${header}" not in ${found}')
}
}
}

View File

@ -38,12 +38,12 @@ pub fn (mut s Server) listen_and_serve() {
if s.handler is DebugHandler {
eprintln('Server handler not set, using debug handler')
}
s.listener = net.listen_tcp(.ip6, ':$s.port') or {
eprintln('Listening on :$s.port failed')
s.listener = net.listen_tcp(.ip6, ':${s.port}') or {
eprintln('Listening on :${s.port} failed')
return
}
s.listener.set_accept_timeout(s.accept_timeout)
eprintln('Listening on :$s.port')
eprintln('Listening on :${s.port}')
s.state = .running
for {
// break if we have a stop signal
@ -55,7 +55,7 @@ pub fn (mut s Server) listen_and_serve() {
// just skip network timeouts, they are normal
continue
}
eprintln('accept() failed, reason: $err; skipping')
eprintln('accept() failed, reason: ${err}; skipping')
continue
}
conn.set_read_timeout(s.read_timeout)
@ -88,7 +88,7 @@ pub fn (s &Server) status() ServerStatus {
fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
defer {
conn.close() or { eprintln('close() failed: $err') }
conn.close() or { eprintln('close() failed: ${err}') }
}
mut reader := io.new_buffered_reader(reader: conn)
@ -100,7 +100,7 @@ fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
req := parse_request(mut reader) or {
$if debug {
// only show in debug mode to prevent abuse
eprintln('error parsing request: $err')
eprintln('error parsing request: ${err}')
}
return
}
@ -108,7 +108,7 @@ fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
if resp.version() == .unknown {
resp.set_version(req.version)
}
conn.write(resp.bytes()) or { eprintln('error sending response: $err') }
conn.write(resp.bytes()) or { eprintln('error sending response: ${err}') }
}
// DebugHandler implements the Handler interface by echoing the request
@ -117,9 +117,9 @@ struct DebugHandler {}
fn (d DebugHandler) handle(req Request) Response {
$if debug {
eprintln('[$time.now()] $req.method $req.url\n\r$req.header\n\r$req.data - 200 OK')
eprintln('[${time.now()}] ${req.method} ${req.url}\n\r${req.header}\n\r${req.data} - 200 OK')
} $else {
eprintln('[$time.now()] $req.method $req.url - 200')
eprintln('[${time.now()}] ${req.method} ${req.url} - 200')
}
mut r := Response{
body: req.data

View File

@ -41,7 +41,7 @@ fn (mut handler MyHttpHandler) handle(req http.Request) http.Response {
handler.counter++
// eprintln('$time.now() | counter: $handler.counter | $req.method $req.url\n$req.header\n$req.data - 200 OK\n')
mut r := http.Response{
body: req.data + ', $req.url'
body: req.data + ', ${req.url}'
header: req.header
}
match req.url.all_before('?') {
@ -71,17 +71,17 @@ fn test_server_custom_handler() {
for server.status() != .running {
time.sleep(10 * time.millisecond)
}
x := http.fetch(url: 'http://localhost:$cport/endpoint?abc=xyz', data: 'my data')!
x := http.fetch(url: 'http://localhost:${cport}/endpoint?abc=xyz', data: 'my data')!
assert x.body == 'my data, /endpoint?abc=xyz'
assert x.status_code == 200
assert x.http_version == '1.1'
y := http.fetch(url: 'http://localhost:$cport/another/endpoint', data: 'abcde')!
y := http.fetch(url: 'http://localhost:${cport}/another/endpoint', data: 'abcde')!
assert y.body == 'abcde, /another/endpoint'
assert y.status_code == 200
assert y.status() == .ok
assert y.http_version == '1.1'
//
http.fetch(url: 'http://localhost:$cport/something/else')!
http.fetch(url: 'http://localhost:${cport}/something/else')!
server.stop()
t.wait()
assert handler.counter == 3