typapp

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2023 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultCtorAnnot = "@ctor"
)

Functions

func Container

func Container() (*dig.Container, error)

func Invoke

func Invoke(fn interface{}) error
Example
package main

import (
	"fmt"
	"log"

	"github.com/kmrtftech/tg-framework/pkg/typapp"
)

func main() {
	typapp.Reset()

	typapp.Provide("", func() string { return "world" })

	err := typapp.Invoke(func(text string) {
		fmt.Printf("hello %s\n", text)
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:
hello world
Example (Second)
package main

import (
	"fmt"
	"log"

	"github.com/kmrtftech/tg-framework/pkg/typapp"
	"go.uber.org/dig"
)

func main() {
	typapp.Reset()

	typapp.Provide("t1", func() string { return "hello" }) // provide same type
	typapp.Provide("t2", func() string { return "world" }) // provide same type

	type param struct {
		dig.In
		Text1 string `name:"t1"`
		Text2 string `name:"t2"`
	}

	printHello := func(p param) {
		fmt.Printf("%s %s\n", p.Text1, p.Text2)
	}

	if err := typapp.Invoke(printHello); err != nil {
		log.Fatal(err)
	}

}
Output:
hello world

func Provide

func Provide(name string, fn interface{})

Provide constructor

func Reset

func Reset()

func SetConstructors

func SetConstructors(c []*Constructor)

func SetContainer

func SetContainer(c *dig.Container)

func StartApp

func StartApp(startFn, stopFn interface{}, exitSigs ...os.Signal) error

StartApp start the service with gracefully stop

Example
package main

import (
	"fmt"
	"log"

	"github.com/kmrtftech/tg-framework/pkg/typapp"
)

func main() {
	typapp.Reset() // make sure constructor and container is empty (optional)
	typapp.Provide("", func() string { return "world" })

	startFn := func(text string) { fmt.Printf("hello %s\n", text) }
	stopFn := func() { fmt.Println("bye2") }

	if err := typapp.StartApp(startFn, stopFn); err != nil {
		log.Fatal(err)
	}

}
Output:
hello world
bye2

Types

type Constructor

type Constructor struct {
	Name string
	Fn   interface{}
}

Constructor details

func Constructors

func Constructors() []*Constructor

type CtorAnnot

type CtorAnnot struct{}

CtorAnnot handle @ctor annotation e.g. `@ctor (name:"NAME")`

func (*CtorAnnot) AnnotationName

func (a *CtorAnnot) AnnotationName() string

func (*CtorAnnot) IsAllowed

func (a *CtorAnnot) IsAllowed(d *typgen.Annotation) bool

func (*CtorAnnot) ProcessAnnotatedFile

func (a *CtorAnnot) ProcessAnnotatedFile(c *typgen.Context, f *typgen.File, annots []*typgen.Annotation) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL