Skip to content

lionelvillard/starlark-go

 
 

Repository files navigation

Starlark in Go with YAML extensions

This project extends Starkark in Go with first class YAML support.

Additional statements

SmallStmt = YamlStmt | ...

YamlStmt = identifier ':' YAMLTail
         | string ':' YAMLTail
         | '-' YAMLTail

YAMLTail is a lexical element corresponding to all lines with the same or greater indendation level as YamlStmt

Examples

YAML Mapping to dictionary

def func1():
  name: max
  cities:
  - SF
  - LA

print(func1()) # print a dictionary

YAML Sequence to list

def func():
  - SF city
  - LA city

print(func1()) # print a sequence

Dot expression and dictionary

The dot expression can be used to get the dictionary value corresponding to the dot expression attribute

def func1():
  name: max
  cities:
  - SF
  - LA

print(func1().name) # print max

Builtin dictionary methods take precedence over attribute selection. For instance, access the attribute values by using either get or an index.

def func1():
  values: ['1', '2']

print(func1()['values'][0]) # print 1

Enclosed Starlark expression in YAML

YAML text enclosed in curly braces { ... } is interpreted as Starlark expression

def func1(d):
  text: the city name is { d.name }

print(func1({'name': 'NY'}).text) # print the city name is NY

Mapping flow style disambiguation

In YAML, flow mappings are denoted by surrounding '{' and '}' characters.

def func1(d):
  text: { d.name } is big

print(func1({'name': 'NY'}).text) # print an error!

You can change this behavior by prefixing { with s.

def func1(d):
  text: s{ d.name } is big

print(func1({'name': 'NY'}).text) # print NY is big

About

Starlark in Go: the Starlark configuration language, implemented in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%