hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Add generators in expression

Open fabricereix opened this issue 1 year ago • 8 comments

fabricereix avatar Aug 13 '24 13:08 fabricereix

@jcamiel @lepapareil We need to choose whether we use parenthesis or not for the generator.

Without parentheses, they look like variables. We should therefore raise an error when defining a variable with a generator name.

POST https://foo.com
```
{{ now }}
```

or with parenthesis

POST https://foo.com
```
{{ now() }}
```

The first one might look a bit more hurly.

fabricereix avatar Aug 13 '24 13:08 fabricereix

Maybe we can take some inspirations from Jetbrains HTTP client file format "dynamic variables":

Dynamic variables generate a value each time you run a request. Their names start with $: $uuid or $random.uuid: generates a universally unique identifier (UUID-v4) $timestamp: generates the current UNIX timestamp $isoTimestamp: generates the current timestamp in ISO-8601 format for the UTC timezone. $randomInt: generates a random integer between 0 and 1000. $random.integer(from, to): generates a random integer between from (inclusive) and to (exclusive), for example random.integer(100, 500). If you provide no parameters, it generates a random integer between 0 and 1000. $random.float(from, to): generates a random floating point number between from (inclusive) and to (exclusive), for example random.float(10.5, 20.3). If you provide no parameters, it generates a random float between 0 and 1000. $random.alphabetic(length): generates a sequence of uppercase and lowercase letters of length length (must be greater than 0). $random.alphanumeric(length): generates a sequence of uppercase and lowercase letters, digits, and underscores of length length (must be greater than 0). $random.hexadecimal(length): generates a random hexadecimal string of length length (must be greater than 0). $random.email: generates a random email address. $exampleServer: is replaced with the IntelliJ IDEA built-in web server, which can be accessed using HTTP Client only.

POST http://localhost/api/post?id={{$uuid}}

{
    "time": {{$timestamp}},
    "price": {{$random.integer(10, 1000)}},
}

Parenthesis doesn't seem hurly though

jcamiel avatar Aug 13 '24 13:08 jcamiel

2 choices then

  • prefix or not with $
  • suffix or not with ()

fabricereix avatar Aug 13 '24 13:08 fabricereix

In a template expression, both Javascript and Kotlin do not prefix their function call with a $. The $ is used for the expression delimiter.

function now() { return new Date()} 
`Time is ${now()}` 

import java.util.*

fun now(): Date {
    return Date()
}
println("time is ${now()}")

fabricereix avatar Aug 13 '24 14:08 fabricereix

Postman makes alos the distinction between "classic" variables and "dynamic variables":

https://learning.postman.com/docs/tests-and-scripts/write-scripts/variables-list/

Use these variables like you would any other variable in Postman. Their values are generated at the time of execution, and their names start with a $ symbol, for example, $guid or $timestamp.

  • Common
Variable Name Description Examples
$guid A uuid-v4 style guid "611c2e81-2ccb-42d8-9ddc-2d0bfa65c1b4"
$timestamp The current UNIX timestamp in seconds 1562757107, 1562757108, 1562757109
$isoTimestamp The current ISO timestamp at zero UTC 2020-06-09T21:10:36.177Z
$randomUUID A random 36-character UUID "6929bb52-3ab2-448a-9796-d6480ecad36b"
  • Text, numbers, and colors
Variable Name Description Examples
$randomAlphaNumeric A random alpha-numeric character 6, "y", "z"
$randomBoolean A random boolean value true, false
$randomInt A random integer between 0 and 1000 802, 494, 200
$randomColor A random color "red", "fuchsia", "grey"
$randomHexColor A random hex value "#47594a", "#431e48", "#106f21"
$randomAbbreviation A random abbreviation SQL, PCI, JSON

etc...

I quite like the visual distinction between dynamic and classic variable, the fact that we're use not to break any existing test and the "common" syntax betwenn Jetbrain HTTP, Postman etc...

VS REST Client uses also the same syntax:

Provide system dynamic variables {{$guid}} {{$randomInt min max}} {{$timestamp [offset option]}} {{$datetime rfc1123|iso8601 [offset option]}} {{$localDatetime rfc1123|iso8601 [offset option]}} {{$processEnv [%]envVarName}} {{$dotenv [%]variableName}} {{$aadToken [new] [public|cn|de|us|ppe] [<domain|tenantId>] [aud:<domain|tenantId>]}}

Note: no parenthesis in VS Rest client also

jcamiel avatar Aug 13 '24 15:08 jcamiel

📆 This PR has been closed because there is no activity (commits/comments) for more than 15 days 😥. Feel free to reopen it with new commits/comments.

hurl-bot avatar Aug 29 '24 03:08 hurl-bot

As discussed, prefixing the generators with a verb could be both explicit and consistent with filters. They can have zero or several parameters.

generate new create get
generateUuid newUuid createUuid getUuid
generateUUID newUUID createUUID getUUID
generateRandomUUID newRandomUUID createRandomUUID getRandomUUID
generateDate newDate createDate getDate
generateRandomDate newRandomDate createRandomDate getRandomDate
generateRandomInt newRandomInt createRandomInt getRandomInt
generateRandomFloat newRandomFloat createRandomFloat getRandomFloat
generateRandomHex newRandomHex createRandomHex getRandomHex
generateRandomColor newRandomColor createRandomColor getRandomColor
generateRandomBoolean newRandomBoolean createRandomBoolean getRandomBoolean

fabricereix avatar Aug 29 '24 09:08 fabricereix

My favorites are new and create.

fabricereix avatar Aug 29 '24 09:08 fabricereix

We could also use "get" for some generators for instance {{ getEnv "HOST" }} ?

jcamiel avatar Aug 30 '24 14:08 jcamiel

My favorites:

  • new for generated/random data
  • get for predefined/inherited data

lepapareil avatar Aug 30 '24 14:08 lepapareil

yes, we could say that calling several timesgetXXX should return the same value

fabricereix avatar Aug 31 '24 14:08 fabricereix

As discussed, we will use function naming rather generator.

fabricereix avatar Sep 06 '24 14:09 fabricereix

/accept

jcamiel avatar Sep 08 '24 08:09 jcamiel

🕗 /accept is running, please wait for completion.

hurl-bot avatar Sep 08 '24 08:09 hurl-bot

🔨 Auto rebase from Orange-OpenSource/hurl/master succeeds, Orange-OpenSource/hurl/feature/add-generator now embeds these commits:

  • 64b7e4cd2 Update crates
  • 56492a251 Update crates
  • 32f142a25 Update crates
  • d50eb5695 Update crates
  • 68cd6f215 Add package-generic-linux-aarch64 to package github workflow
  • 108417073 Update actions
  • 86c792913 Update crates
  • 6bf371a05 Update npm Hurl 5.0.1 package.

hurl-bot avatar Sep 08 '24 08:09 hurl-bot

🕗 /accept is still running, please wait for completion.

hurl-bot avatar Sep 08 '24 08:09 hurl-bot

✅ Pull request merged and closed by jcamiel with fast forward merge..

# List of commits merged from Orange-OpenSource/hurl/feature/add-generator branch into Orange-OpenSource/hurl/master branch:

  • 5af5889b7 Add functions in expression

hurl-bot avatar Sep 08 '24 08:09 hurl-bot