Beispiel #1
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)
Beispiel #2
0
    with m.func("Run", LazyFormat("{}: string", filename), return_="error"):
        config = load_config(m, filename)
        m.return_(doSomething(config))  # or "return nil"
    return m


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


# ASTの変換でできないかな。。
# そしてこうなるならもう関数として括る必要もないのでは?
# やっていることは
# - "=", ":=" の使い分け (constがほしい)
def load_configX(filename: str) -> Symbol:
    from xxx import LoadConfig

    config = LoadConfig(filename)
    return config


print(run(Module()))