https://github.com/yaml/yamlscript-go
Go Binding for libyamlscript
https://github.com/yaml/yamlscript-go
Last synced: 10 months ago
JSON representation
Go Binding for libyamlscript
- Host: GitHub
- URL: https://github.com/yaml/yamlscript-go
- Owner: yaml
- License: mit
- Created: 2024-07-05T17:11:14.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-07-18T18:40:04.000Z (11 months ago)
- Last Synced: 2025-08-02T00:53:40.755Z (11 months ago)
- Language: Go
- Size: 22.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: ReadMe.md
Awesome Lists containing this project
README
YS / YAMLScript
===============
Add Logic to Your YAML Files
## Synopsis
Load `file.yaml` with YS:
```yaml
!YS-v0:
# Get data from external sources:
names-url =:
'github:dominictarr/random-name/first-names.json'
name-list =: names-url:curl:json/load
# Data object with literal keys and generated values:
name:: name-list:shuffle:first
aka:: name-list:rand-nth
age:: &num 2 * 3 * 7
color:: &hue
rand-nth: qw(red green blue yellow)
title:: "$(*num) shades of $(*hue)."
```
and get:
```json
{
"name": "Dolores",
"aka": "Anita",
"age": 42,
"color": "green",
"title": "42 shades of green."
}
```
## Description
[YS](https://yamlscript.org) is a functional programming language with a clean
YAML syntax.
YS can be used for enhancing ordinary [YAML](https://yaml.org) files with
functional operations, such as:
* Import (parts of) other YAML files to any node
* String interpolation including function calls
* Data transforms including ones defined by you
This YS library should be a drop-in replacement for your current YAML loader!
Most existing YAML files are already valid YS files.
This means that YS works as a normal YAML loader, but can also evaluate
functional expressions if asked to.
Under the hood, YS code compiles to the Clojure programming language.
This makes YS a complete functional programming language right out of the box.
Even though YS compiles to Clojure, and Clojure compiles to Java, there is no
dependency on Java or the JVM.
YS is compiled to a native shared library (`libys.so`) that can be used
by any programming language that can load shared libraries.
To see the Clojure code that YS compiles to, you can use the YS
CLI binary `ys` to run:
```text
$ ys --compile file.ys
(let
[names-url "https://raw.githubusercontent.com/dominictarr/random-name/master/first-names.json"
name-list (json/load (curl names-url))]
(%
"name" (first (shuffle name-list))
"aka" (rand-nth name-list)
"age" (_& 'num (mul+ 2 3 7))
"color" (_& 'hue (rand-nth (qw red green blue yellow)))
"title" (str (_** 'num) " shades of " (_** 'hue) ".")))
```
## Go Usage
In `go.mod`:
```go
require github.com/yaml/yamlscript-go v0.2.2
```
File `prog.go`:
```go
package main
package main
import (
"fmt"
"github.com/yaml/yamlscript-go"
)
func main() {
data, err := yamlscript.Load("a: [b, c]")
if err != nil {
return
}
fmt.Println(data)
}
```
## Installation
You can install this module like any other Go module:
```bash
$ go get github.com/yaml/yamlscript-go@v0.2.2
```
but you will need to have a system install of `libys.so`.
One simple way to do that is with:
```bash
$ curl https://yamlscript.org/install | bash
```
> Note: The above command will install the latest version of the YAMLScript
command line utility, `ys`, and the shared library, `libys.so`, into
`~/local/bin` and `~/.local/lib` respectively.
See for more info.
### Environment Variables
At the current time, you will need to set 3 environment variables to use the
module:
```bash
export CGO_CFLAGS="-I $HOME/.local/include"
export CGO_LDFLAGS="-L $HOME/.local/lib"
export LD_LIBRARY_PATH="$HOME/.local/lib"
```
## See Also
* [YS Web Site](https://yamlscript.org)
* [YS Blog](https://yamlscript.org/blog)
* [YS Source Code](https://github.com/yaml/yamlscript)
* [YS Samples](https://github.com/yaml/yamlscript/tree/main/sample)
* [YS Programs](https://rosettacode.org/wiki/Category:YAMLScript)
* [YAML](https://yaml.org)
* [Clojure](https://clojure.org)
## Authors
* [Ingy döt Net](https://github.com/ingydotnet)
* [Andrew Pam](https://github.com/xanni)
## License & Copyright
Copyright 2022-2025 Ingy döt Net
This project is licensed under the terms of the `MIT` license.
See [LICENSE](https://github.com/yaml/yamlscript/blob/main/License) for
more details.