Exemplo n.º 1
0
Arquivo: editor.go Projeto: kego/ke
func Editor(ctx context.Context, env *envctx.Env) (source []byte, err error) {

	g := builder.New("main")

	g.Imports.Anonymous("kego.io/system")
	g.Imports.Anonymous(env.Path)
	for _, p := range env.Aliases {
		g.Imports.Anonymous(p)
	}
	/*
		func main() {
			if err := client.Start(); err != nil {
				fmt.Println(err.Error())
			}
		}
	*/
	g.Println("func main() {")
	{
		clientStart := builder.Reference("kego.io/editor/client", "Start", env.Path, g.Imports.Add)
		g.Println("if err := ", clientStart, "(); err != nil {")
		{
			fmtPrintln := builder.Reference("fmt", "Println", env.Path, g.Imports.Add)
			g.Println(fmtPrintln, "(err.Error())")
		}
		g.Println("}")
	}
	g.Println("}")

	b, err := g.Build()
	if err != nil {
		return nil, kerr.Wrap("CBTOLUQOGL", err)
	}
	return b, nil
}
Exemplo n.º 2
0
Arquivo: structs.go Projeto: kego/ke
func Structs(ctx context.Context, env *envctx.Env) (source []byte, err error) {

	scache := sysctx.FromContext(ctx)

	pcache, ok := scache.Get(env.Path)
	if !ok {
		return nil, kerr.New("DQVQWTKRSK", "%s not found in sys ctx", env.Path)
	}
	types := pcache.Types
	exports := pcache.Exports

	g := builder.New(env.Path)

	infoBytes, _ := json.Marshal(InfoStruct{Path: env.Path, Hash: env.Hash})

	g.SetPackageComment("info:" + string(infoBytes))
	g.SetIntroComment(`ke: {"file": {"notest": true}}`)

	for _, name := range types.Keys() {
		t, ok := types.Get(name)
		if !ok {
			// ke: {"block": {"notest": true}}
			continue
		}
		typ := t.Type.(*system.Type)

		isRule := typ.Id.IsRule()
		isNativeCollection := typ.IsNativeCollection() && typ.Alias == nil

		if !typ.Interface && !typ.Custom {
			if typ.Alias != nil {
				if err := printAliasDefinition(ctx, env, g, typ); err != nil {
					return nil, kerr.Wrap("TRERIECOEP", err)
				}
				if kind, _ := typ.Kind(ctx); kind == system.KindValue {
					if err := printValueMethod(ctx, env, g, typ); err != nil {
						return nil, kerr.Wrap("PGDUJQVQGR", err)
					}
				}
			} else {
				if err := printStructDefinition(ctx, env, g, typ); err != nil {
					return nil, kerr.Wrap("XKRYMXUIJD", err)
				}
			}
		}

		if !typ.Interface && !isRule && !isNativeCollection {
			printInterfaceDefinition(ctx, env, g, typ)
			printInterfaceImplementation(ctx, env, g, typ)
		}

		if !isRule && !isNativeCollection {
			printInterfaceUnpacker(ctx, env, g, typ)
		}

		if !typ.Custom && !typ.Interface && !isNativeCollection {
			if err := printUnpacker(ctx, env, g, typ); err != nil {
				return nil, kerr.Wrap("YJNWUAUKXI", err)
			}
			if err := printRepacker(ctx, env, g, typ); err != nil {
				return nil, kerr.Wrap("NCFFXUHYNY", err)
			}
		}

	}
	printInitFunction(ctx, env, g, types)

	for _, name := range exports.Keys() {
		export, ok := exports.Get(name)
		if !ok {
			// ke: {"block": {"notest": true}}
			continue
		}
		if err := printExportFunction(ctx, env, g, export); err != nil {
			return nil, kerr.Wrap("YJHRXIASNO", err)
		}
	}

	b, err := g.Build()
	if err != nil {
		return nil, kerr.Wrap("XKYHSDKBEP", err)
	}
	return b, nil
}