Ejemplo n.º 1
0
def run(m: Module) -> Module:
    filename = Symbol("filename")
    doSomething = Symbol("doSomething")  # todo: import

    with m.func("Run", LazyFormat("{}: string", filename), return_="error"):
        config = load_config(m, filename)
        m.return_(doSomething(config))  # or "return nil"
    return m
Ejemplo n.º 2
0
def load_config(m: Module, filename: Symbol) -> Symbol:
    LoadConfig = Symbol("LoadConfig")  # todo: import
    config = Symbol("config")
    err = Symbol("err")

    m.stmt("{}, {} := {}", config, err, LoadConfig(filename))
    with m.if_(LazyFormat("{} != nil", err)):
        m.return_(err)
    return config
Ejemplo n.º 3
0
import typing as t
from appkit import Config, Error, Cleanup, gen, Symbol
from prestring.go import Module


class Foo:
    __gomodule__ = "github.com/podhmo/appkit/foo"


def FromConfig(c: Config) -> Foo:
    pass


c = Symbol("c", Config)
m = Module()

with m.func("run", return_="error"):
    m.stmt("filename := *config")
    m.stmt("c, err := conf.LoadConfig(filename)")
    with m.if_("err != nil"):
        m.return_("err")
    m = gen(m, c, FromConfig)
    m.stmt("fooOb := foo.FromConfig(c)")
    m.return_("use(fooOb)")
print(m)
Ejemplo n.º 4
0
from prestring.go import Module
m = Module()
m.package('main')

with m.import_group() as im:
    im.import_("fmt")

with m.func('Fizzbuzz', 'v int', return_='(string, error)'):
    with m.if_("v == 1"):
        m.return_('"1", nil')
    for i in range(2, 101):
        with m.elif_("v == {}".format(i)):
            if i % 15 == 0:
                m.return_('"fizzbuzz", nil')
            elif i % 3 == 0:
                m.return_('"fizz", nil')
            elif i % 5 == 0:
                m.return_('"buzz", nil')
            else:
                m.return_('"{}", nil'.format(i))
    with m.else_():
        m.return_('"", fmt.Errorf("unsupported value: %q", v)')

print(m)