Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests, Fixing bug for l2config absent, updating documentation and adding examples #27

Merged
merged 12 commits into from
Jul 23, 2023
Merged
Binary file removed docs/Lama2/docs/explanation/l2envOverideL2config.png
Binary file not shown.
56 changes: 32 additions & 24 deletions docs/Lama2/docs/explanation/l2format.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ Cookies are specified in a `Cookie` header as follows:
Cookie:'sessionid=foo;another-cookie=bar'
```

### API variables can be defined in `apirequest.l2`

L2 uses the variables declared inside the `.l2` file and makes the request
### Environment Variables

#### API variables can be defined in `apirequest.l2`
Variables are declared within the JS processor block and serve as dynamic placeholders for data used in API requests.
By utilizing these variables, L2 enables flexibility and reusability in defining API endpoints and data payloads.

Example `login.l2`:

Expand All @@ -103,41 +106,45 @@ ${REMOTE}/login
Get [Source Files](https://github.com/HexmosTech/Lama2/tree/main/examples/0021_varjson_variable/0021_varjson_variable.l2)


### API environment variables can be defined locally in `l2.env`
#### API environment variables can be defined locally in `l2.env`
L2 provides a convenient way to define environment variables through the l2.env file.
This file is automatically searched for in the present directory,
and its contents are loaded to create a set of variables (local).

`l2.env` is searched for, from the present directory and variables(local) are loaded from this file.
In the `l2.env` file, you can specify environment-specific values for variables used in your L2 scripts, such as URLs, authentication tokens, or any other data that may vary depending on the environment in which the API requests are executed.

Example `l2.env`:
![l2.env at API level](../tutorials/l2env.png)

```
export PHOTO=`base64 aadhaarlarge.jpg`
export AHOST="http://localhost:8000"
```
Go to [Example](../tutorials/examples.md#case-1-l2env-adjacent-to-an-api-file)

Get [Source Files](https://github.com/HexmosTech/Lama2/tree/main/examples/0004_env_switch_root)
Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0023_l2env_declare)

### API environment variables can be defined at root using `l2config.env`
`l2config.env` is searched for, from the present directory to all its ancestors (upto `/`) and
variables(root) are loaded from this file.
Example `l2config.env`:

```
export PHOTO=`base64 aadhaarsmall.jpg`
export AHOST="http://localhost:8001"
```
#### API environment variables can be defined at project root using `l2config.env`
The `l2config.env` file serves as a centralized storage for environment variables located at the project root, streamlining the management of configuration settings across all L2 scripts. With this file present, every L2 script within the project automatically inherits the defined variables, effectively eliminating the necessity to duplicate configurations in individual subdirectories using `l2.env`.

The search for `l2config.env` extends from the present directory up to the root directory (`/`). During this process, the variables defined in the root file are loaded and made available for use in all relevant scripts. This approach significantly enhances efficiency and maintainability, as it ensures consistent settings throughout the project while reducing redundancy in configuration data.

Get [Source Files](https://github.com/HexmosTech/Lama2/tree/main/examples/0019_env_switch_global_root)
![l2config.env at Project root level](../tutorials/l2configAtRoot.png)

### If `l2config.env`(root) variables are redeclared in `l2.env`(local)
Go to [Example](../tutorials/examples.md#case-2-root-variables)

The local variable's value is taken into consideration regardless of both files residing in same directory
Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0022_l2config_declare)

Get [Source Files](https://github.com/HexmosTech/Lama2/tree/main/examples/0020_override_project_root_local)

![Override of l2config.env with l2.env variable](l2envOverideL2config.png)
#### If `l2config.env`(root) variables are redeclared in `l2.env`(local)
In situations where both root and local variables share the same variable name, the local variable takes precedence over the root variable. This behavior remains consistent, even if both `l2config.env` (root) and `l2.env` (local) files reside in the same directory.

The local variable's value will always be considered over the root variable, ensuring that specific configurations defined at the local level effectively override any corresponding settings present in the root file. This approach provides developers with granular control and flexibility in tailoring environment variables to suit specific needs within different parts of the project while maintaining the overall structure and organization of configuration settings.

#### The environment file can load results of commands
![l2config.env at Project root level](../tutorials/l2envOverideL2config.png)

Go to [Example](../tutorials/examples.md#case-3-override-root-variable-with-local-variable)

Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0020_override_project_root_local)


### The environment file can load results of commands

Use the backtick notation `\`command\`` to place the results of
commands into environment variables:
Expand All @@ -148,6 +155,7 @@ export PHOTO=`base64 image.jpeg`

One can load the `PHOTO` variable in API files.


### Chain requests through Javascript blocks

*Lama2* supports plain Javascript (JS) blocks
Expand Down
14 changes: 7 additions & 7 deletions docs/Lama2/docs/reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ From a high level, how does it work now?
3. Initialize Javascript VM for executing JS blocks
4. For each block
1. If block is JS Processor block
1. Execute JS code in VM
1. Execute JS code in VM
2. Else if block is Requestor block
1. Replace variables with values in the following order
1. Try fetch variable from Javascript VM
2. If (1) fails, try fetch Local env variable from `l2.env`
3. Try fetch root env variable from `l2config.env`
2. Use the processed elements to create an httpie-go request
3. Fetch response
1. Replace variables with values in the following order
1. Try fetch variable from Javascript VM
2. If (1) fails, try fetch Local env variable from `l2.env`
3. Try fetch root env variable from `l2config.env`
2. Use the processed elements to create an httpie-go request
3. Fetch response
5. If necessary, write the last transaction to `.json` file
12 changes: 6 additions & 6 deletions docs/Lama2/docs/reference/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var DataInputType string
```

<a name="CustomPairMerge"></a>
## func [CustomPairMerge](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L29>)
## func [CustomPairMerge](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L30>)

```go
func CustomPairMerge(destination, source interface{}) interface{}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (p *Lama2Parser) AnyType() (*gabs.Container, error)
AnyType is the top\-most element of a JSON structure It consists of Complex and Primitive Types

<a name="Lama2Parser.Boolean"></a>
### func \(\*Lama2Parser\) [Boolean](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L126>)
### func \(\*Lama2Parser\) [Boolean](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L127>)

```go
func (p *Lama2Parser) Boolean() (*gabs.Container, error)
Expand Down Expand Up @@ -320,7 +320,7 @@ func (p *Lama2Parser) Lama2File() (*gabs.Container, error)


<a name="Lama2Parser.List"></a>
### func \(\*Lama2Parser\) [List](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L69>)
### func \(\*Lama2Parser\) [List](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L70>)

```go
func (p *Lama2Parser) List() (*gabs.Container, error)
Expand All @@ -329,7 +329,7 @@ func (p *Lama2Parser) List() (*gabs.Container, error)
List is a slightly lenient version of standard JSON list. In Lama2 List, it is OK to have a trailing comma after the last element \(whereas in strict JSON, it is not OK to have trailing comma\)

<a name="Lama2Parser.Map"></a>
### func \(\*Lama2Parser\) [Map](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L37>)
### func \(\*Lama2Parser\) [Map](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L38>)

```go
func (p *Lama2Parser) Map() (*gabs.Container, error)
Expand All @@ -347,7 +347,7 @@ func (p *Lama2Parser) Multipart() (*gabs.Container, error)


<a name="Lama2Parser.Null"></a>
### func \(\*Lama2Parser\) [Null](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L142>)
### func \(\*Lama2Parser\) [Null](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L143>)

```go
func (p *Lama2Parser) Null() (*gabs.Container, error)
Expand All @@ -374,7 +374,7 @@ func (p *Lama2Parser) OneNine() (*gabs.Container, error)


<a name="Lama2Parser.Pair"></a>
### func \(\*Lama2Parser\) [Pair](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L105>)
### func \(\*Lama2Parser\) [Pair](<https://github.com/HexmosTech/Lama2/blob/main/parser/jsonparser.go#L106>)

```go
func (p *Lama2Parser) Pair() (*gabs.Container, error)
Expand Down
6 changes: 3 additions & 3 deletions docs/Lama2/docs/reference/preprocess.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ExpandURL(block *gabs.Container, vm *goja.Runtime)


<a name="GetL2EnvVariables"></a>
## func [GetL2EnvVariables](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L170>)
## func [GetL2EnvVariables](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L169>)

```go
func GetL2EnvVariables(dir string) ([]byte, error)
Expand All @@ -79,7 +79,7 @@ func GetL2EnvVariables(dir string) ([]byte, error)


<a name="GetLamaFileAsString"></a>
## func [GetLamaFileAsString](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L201>)
## func [GetLamaFileAsString](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L200>)

```go
func GetLamaFileAsString(path string) string
Expand All @@ -88,7 +88,7 @@ func GetLamaFileAsString(path string) string


<a name="LamaFile"></a>
## func [LamaFile](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L215>)
## func [LamaFile](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L214>)

```go
func LamaFile(inputFile string) (string, string)
Expand Down
36 changes: 32 additions & 4 deletions docs/Lama2/docs/tutorials/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
The `l2` command provides some helpful options for
extension developers. The options are:

1. `--nocolor` or `-n` disables colored output in httpie-go (in CLI);
1. `--output=<target.json` or `-o` writes a structured JSON
1. `--env` or `-e` outputs a JSON of environment variables (in CLI);
2. `--nocolor` or `-n` disables colored output in httpie-go (in CLI);
3. `--output=<target.json` or `-o` writes a structured JSON
output to the target file (without colors). The following is the content
structure:

Expand All @@ -19,13 +20,15 @@ structure:
- `headers`: A string consisting of `HTTP header: Value` pairs separated by newline.
- `body`: A string containing the HTTP response. Usually a JSON or HTML response.


!!! note

Right now, all the three values in the JSON are strings. In the future, we may transform the values further to provide a more parse-friendly structure.

## The Command
## The Commands

Combining the above two options, we get:
### Execute current file
Combining the options `-n` and `-o`, we get:

```bash
l2 -n -o /tmp/lama2.json my_api.l2
Expand All @@ -40,6 +43,31 @@ display the contents to users appropriately. For an
example, see [Lama2 for VSCode](https://github.com/HexmosTech/Lama2Code)
(also see [Marketplace page](https://marketplace.visualstudio.com/items?itemName=hexmos.Lama2)).

### Providing environment variable autocompletion
To obtain a combined JSON representation of environment variables from `l2.env` and `l2config.env`, use option `-e` or `--env`. This will output the result to `stdout`.
```bash
l2 -e /path/to/my_api.l2
```
```json
{
"AHOST": {
"src": "l2env",
"val": "http://127.0.0.1:8000"
},
"BHOST": {
"src": "l2configenv",
"val": "https://httpbin.org"
}
}
```
The extension author can simply read the `stdout` after executing the command, and display the variables to users appropriately.

![l2envvariable variable](l2envvariable.png)

![l2configvariable variable](l2configvariable.png)

Go to [Example](../tutorials/examples.md#case-3-override-root-variable-with-local-variable)

## Syntax Highlighting

The VSCode plugin implements a rudimentary syntax highlighting for `.l2` files. We use [Iro](https://eeyo.io/iro/documentation/) for syntax grammar generation. Find [more details](https://github.com/HexmosTech/Lama2/tree/main/syntax/README.md) if interested.
57 changes: 50 additions & 7 deletions docs/Lama2/docs/tutorials/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,79 @@ c=d
Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0003_comment.l2)

## Environment Variables: Switch base URL

### Case 1: `l2.env` adjacent to an API file

For any given `.l2` file, one can place an `l2.env` file to store relevant variables.
These variables will be available to be used within the API file

**l2.env**
**project_folder/api/l2.env**
```
export AHOST="http://127.0.0.1:8000"
```

**project_folder/api/get_users.l2**
```
export LOCAL="http://localhost:8000"
export REMOTE="http://httpbin.org"
GET
${AHOST}/users
```

![l2.env at API level](l2env.png)

Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0023_l2env_declare)


### Case 2: Root variables

In Lama2, you can have a large number of API files stored in a hierarchical folder configuration.
The root of such a project can be signified through `l2config.env`:

Within such a structure, you can have an API file anywhere, which can use variables defined in the root variables:
**l2config.env**

**project_folder/l2config.env**
```
export AHOST="https://httpbin.org"
export BHOST="https://google.com"
```
export LOCAL="http://localhost:8000"
export REMOTE="http://httpbin.org"

**project_folder/api/get_users.l2**
```
GET
${AHOST}/users
```

![l2config.env at Project root level](l2configAtRoot.png)

Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0022_l2config_declare)


### Case 3: Override Root variable with local variable
In this structure, if a variable is declared in both l2config.env and l2.env, the value from l2.env takes precedence.

**project_folder/l2config.env**
```
export AHOST=`echo NO URL`
export BHOST="https://httpbin.org"
```

**project_folder/api/l2.env**
```
export AHOST="http://127.0.0.1:8000"
```

**project_folder/api/get_users.l2**
```
GET
${AHOST}/users
```

![Override of l2config.env with l2.env variable](l2envOverideL2config.png)

![l2envvariable variable](l2envvariable.png)

![l2configvariable variable](l2configvariable.png)

Get [Source File](https://github.com/HexmosTech/Lama2/tree/main/examples/0020_override_project_root_local)

## Headers

Use `key:value` format to specify headers.
Expand Down
1 change: 1 addition & 0 deletions docs/Lama2/docs/tutorials/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Application Options:
-o, --output= Path to output JSON file to store logs, headers and result
-v, --verbose Show verbose debug information
-n, --nocolor Disable color in httpie output
-e --env Get a JSON of environment variables
-h, --help Usage help for Lama2
--version Print Lama2 binary version

Expand Down
Binary file added docs/Lama2/docs/tutorials/l2configAtRoot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/Lama2/docs/tutorials/l2configvariable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/Lama2/docs/tutorials/l2env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/Lama2/docs/tutorials/l2envOverideL2config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/Lama2/docs/tutorials/l2envvariable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/Lama2/site/explanation/l2envOverideL2config.png
Binary file not shown.
Loading