An open API service indexing awesome lists of open source software.

https://github.com/ermos/strlang

🏗️ fluent string builder interface for generate code easily with go
https://github.com/ermos/strlang

Last synced: over 1 year ago
JSON representation

🏗️ fluent string builder interface for generate code easily with go

Awesome Lists containing this project

README

          

# strlang
> 🏗️ fluent string builder interface for generate code easily with go

`strlang` is a Go package for building string-based programming languages.
It provides a simple way to generate strings of code that can be used
in different programming languages.

[![Go Reference](https://pkg.go.dev/badge/github.com/ermos/strlang.svg)](https://pkg.go.dev/github.com/ermos/strlang)
[![Latest tag](https://img.shields.io/github/v/tag/ermos/strlang?label=latest)](https://github.com/ermos/strlang/tags)
[![Go Report Card](https://goreportcard.com/badge/github.com/ermos/strlang)](https://goreportcard.com/report/github.com/ermos/strlang)
[![Maintainability](https://api.codeclimate.com/v1/badges/4d4e0548b083618eba7d/maintainability)](https://codeclimate.com/github/ermos/strlang/maintainability)
[![codecov](https://codecov.io/github/ermos/strlang/branch/main/graph/badge.svg?token=52FCLZVA0B)](https://codecov.io/github/ermos/strlang)
## 🛠️ Installation

To use `strlang` in your Go project, you need to have Go installed on your system.
Once you have Go installed, you can use the following command to install the package:

```go
go get -u github.com/ermos/strlang
```

## 📚 Examples
Here are some examples of how to use `strlang` to generate code in different programming languages.
If your wanted programming language isn't implemented, you can use directly the default builder.

### Javascript
Here is an example of how to use Strlang to generate JavaScript code:

#### Code
```go
package main

import (
"github.com/ermos/strlang"
"fmt"
)

func main() {
js := strlang.NewJavascript()

js.Object("const", "person", func() {
js.WriteStringln(`name: "John",`)
js.WriteStringln(`age: 31,`)
js.WriteStringln(`city: "New York"`)
})

js.If("person.age > 18", func() {
js.WriteStringln(`console.log("John is an adult");`)
})
js.Else(func() {
js.WriteStringln("console.log('John is not an adult');")
})

fmt.Println(js.String())
}
```

#### Output
```javascript
const person = {
name: "John",
age: 31,
city: "New York"
};

if (person.age > 18) {
console.log("John is an adult");
} else {
console.log("John is not an adult");
}
```

### PHP
Here is an example of how to use Strlang to generate PHP code:

#### Code
```go
package main

import (
"github.com/ermos/strlang"
"fmt"
)

func main() {
php := strlang.NewPHP("App/Models")

php.Class("Person", func() {
php.ClassFunc("public", "__construct", "$name, $age", "", func() {
php.WriteStringln(`$this->name = $name;`)
php.WriteStringln(`$this->age = $age;`)
})
})

fmt.Println(php.String())
}
```

#### Output
```php
name = $name;
$this->age = $age;
}
}
```

## 💡 Usage

`strlang` provides a set of builders for each programming language.
Each builder has methods that allow you to build code constructs in that language.
You can use the `New` method of your wanted language (example for javascript: `NewJavascript()`)
functions to create instances of the corresponding builders.
Once you have a builder instance, you can use its methods to build code constructs.

For more information about the available methods and how to use them, please refer to the package documentation.

## 🤝 Contributing

Contributions to `strlang` are always welcome!
If you find a bug or have a feature request, please open an issue on GitHub.
If you want to contribute code, please fork the repository and submit a pull request.