typapp

package
v0.11.7 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MIT Imports: 7 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultCtorAnnot = "@ctor"
)

Functions

func Container added in v0.10.15

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

func Invoke added in v0.11.4

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

import (
	"fmt"
	"log"

	"github.com/typical-go/typical-go/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/typical-go/typical-go/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 added in v0.9.50

func Provide(name string, fn interface{})

Provide constructor

func Reset added in v0.10.14

func Reset()

func SetConstructors added in v0.11.4

func SetConstructors(c []*Constructor)

func SetContainer added in v0.11.4

func SetContainer(c *dig.Container)

func StartApp added in v0.11.5

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

StartApp start the service with gracefully stop

Example
package main

import (
	"fmt"
	"log"

	"github.com/typical-go/typical-go/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 added in v0.9.45

type Constructor struct {
	Name string
	Fn   interface{}
}

Constructor details

func Constructors added in v0.11.4

func Constructors() []*Constructor

type CtorAnnot added in v0.11.4

type CtorAnnot struct{}

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

func (*CtorAnnot) AnnotationName added in v0.11.6

func (a *CtorAnnot) AnnotationName() string

func (*CtorAnnot) IsAllowed added in v0.11.6

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

func (*CtorAnnot) ProcessAnnotatedFile added in v0.11.7

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