Skip to content

Commit

Permalink
style: improve formatting of string concatenation (#62)
Browse files Browse the repository at this point in the history
My first reading of

    &"

is the beginning of a strformat [1] format string, like

    import std/strformat

    const msg = "hello"
    doAssert &"{msg}\n" == "hello\n"

Add spaces around the string concatenation operator `&` to improve
readability.

Also reformat a little, and change `.toLower` to `.toLower()`. We
generally use the parentheses elsewhere to visually distinguish calls
from field accesses.

[1] https://nim-lang.org/docs/strformat.html
  • Loading branch information
ee7 authored Apr 8, 2024
1 parent e46d24a commit e8f1661
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion nimutils/awsclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ proc newAwsClient*(creds: AwsCredentials, region,
let
# TODO - use some kind of template and compile-time variable to put the correct kernel used to build the sdk in the UA?
httpclient = createHttpClient(
userAgent = "nimaws-sdk/0.3.3; "&defUserAgent.replace(" ", "-").toLower&"; darwin/16.7.0",
userAgent = "nimaws-sdk/0.3.3; " & defUserAgent.replace(" ", "-").toLower() & "; darwin/16.7.0",
)
scope = AwsScope(date: getAmzDateString(), region: region, service: service)

Expand Down
8 changes: 5 additions & 3 deletions nimutils/s3client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ proc newS3Client*(creds: AwsCredentials, region: string = defaultRegion,
host: string = awsURI, timeoutMilliseconds = 1000): S3Client =
let
# TODO - use some kind of template and compile-time variable to put the correct kernel used to build the sdk in the UA?
httpclient = newHttpClient("nimaws-sdk/0.3.3; "&defUserAgent.replace(" ",
"-").toLower&"; darwin/16.7.0", timeout = timeoutMilliseconds)
httpclient = newHttpClient(
"nimaws-sdk/0.3.3; " & defUserAgent.replace(" ", "-").toLower() & "; darwin/16.7.0",
timeout = timeoutMilliseconds,
)
scope = AwsScope(date: getAmzDateString(), region: region, service: "s3")

var
Expand All @@ -40,7 +42,7 @@ proc newS3Client*(creds: AwsCredentials, region: string = defaultRegion,
if mhost.len > 0:
if mhost.find("http") == -1:
echo "host should be a valid URI assuming http://"
mhost = "http://"&host
mhost = "http://" & host
else:
mhost = awsURI
endpoint = parseUri(mhost)
Expand Down
6 changes: 3 additions & 3 deletions nimutils/sigv4.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const

# Some convenience operators, for fun and aesthetics
proc `$`(s: AwsScope): string =
return s.date[0..7]&"/"&s.region&"/"&s.service
return s.date[0..7] & "/" & s.region & "/" & s.service

proc `!$`(s: string): string =
return sha256Hex(s)
Expand Down Expand Up @@ -154,8 +154,8 @@ proc create_signature*(key: string, sts: string): string =
proc create_signing_key*(secret: string, scope: AwsScope,
termination: string = term): string =
# (a ?$ b) => $hmac_sha256(a,b)
return ("AWS4"&secret) ?$ scope.date[0..7] ?$ scope.region ?$ scope.service ?$ termination
# ? cleaner than $hmac_sha256($hmac_sha256($hmac_sha256($hmac_sha256("AWS4"&secret, date[0..7]),region),service),termination) ?
return ("AWS4" & secret) ?$ scope.date[0..7] ?$ scope.region ?$ scope.service ?$ termination
# ? cleaner than $hmac_sha256($hmac_sha256($hmac_sha256($hmac_sha256("AWS4" & secret, date[0..7]),region),service),termination) ?

proc create_authorization_header*(id: string, scope: AwsScope,
signed_head: string, sig: string, opts: (string, string) = (alg,
Expand Down
8 changes: 5 additions & 3 deletions nimutils/stsclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ proc newStsClient*(creds: AwsCredentials,
timeoutMilliseconds = 1000): StsClient =
let
# TODO - use some kind of template and compile-time variable to put the correct kernel used to build the sdk in the UA?
httpclient = newHttpClient("nimaws-sdk/0.3.3; "&defUserAgent.replace(" ", "-").toLower&"; darwin/16.7.0",
timeout = timeoutMilliseconds)
httpclient = newHttpClient(
"nimaws-sdk/0.3.3; " & defUserAgent.replace(" ", "-").toLower() & "; darwin/16.7.0",
timeout = timeoutMilliseconds,
)
scope = AwsScope(date: getAmzDateString(), region: region, service: "sts")

var
Expand All @@ -25,7 +27,7 @@ proc newStsClient*(creds: AwsCredentials,
if mhost.len > 0:
if mhost.find("http") == -1:
echo "host should be a valid URI assuming http://"
mhost = "http://"&host
mhost = "http://" & host
else:
mhost = awsURI
endpoint = parseUri(mhost)
Expand Down

0 comments on commit e8f1661

Please sign in to comment.