beego: Index | Files

package bean

import ""

bean is a basic package it should not depend on other modules except common module, log module and config module


Package Files

context.go doc.go factory.go metadata.go tag_auto_wire_bean_factory.go time_type_adapter.go type_adapter.go


const DefaultValueTagKey = "default"

type ApplicationContext Uses

type ApplicationContext interface {

ApplicationContext define for future when we decide to support DI, IoC, this will be core API

type AutoWireBeanFactory Uses

type AutoWireBeanFactory interface {
    // AutoWire will wire the bean.
    AutoWire(ctx context.Context, appCtx ApplicationContext, bean interface{}) error

AutoWireBeanFactory wire the bean based on ApplicationContext and context.Context

type BeanMetadata Uses

type BeanMetadata struct {
    // Fields: field name => field metadata
    Fields map[string]*FieldMetadata

BeanMetadata, in other words, bean's config. it could be read from config file

type FieldMetadata Uses

type FieldMetadata struct {
    // default value in string format
    DftValue string

FieldMetadata contains metadata

type TagAutoWireBeanFactory Uses

type TagAutoWireBeanFactory struct {
    // we allow user register their TypeAdapter
    Adapters map[string]TypeAdapter

    // FieldTagParser is an extension point which means that you can custom how to read field's metadata from tag
    FieldTagParser func(field reflect.StructField) *FieldMetadata

TagAutoWireBeanFactory wire the bean based on Fields' tag if field's value is "zero value", we will execute injection see reflect.Value.IsZero() If field's kind is one of(reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice reflect.UnsafePointer, reflect.Array, reflect.Uintptr, reflect.Complex64, reflect.Complex128 reflect.Ptr, reflect.Struct), it will be ignored

func NewTagAutoWireBeanFactory Uses

func NewTagAutoWireBeanFactory() *TagAutoWireBeanFactory

NewTagAutoWireBeanFactory create an instance of TagAutoWireBeanFactory by default, we register Time adapter, the time will be parse by using layout "2006-01-02 15:04:05" If you need more adapter, you can implement interface TypeAdapter

func (*TagAutoWireBeanFactory) AutoWire Uses

func (t *TagAutoWireBeanFactory) AutoWire(ctx context.Context, appCtx ApplicationContext, bean interface{}) error

AutoWire use value from appCtx to wire the bean, or use default value, or do nothing

type TimeTypeAdapter Uses

type TimeTypeAdapter struct {
    Layout string

TimeTypeAdapter process the time.Time

func (*TimeTypeAdapter) DefaultValue Uses

func (t *TimeTypeAdapter) DefaultValue(ctx context.Context, dftValue string) (interface{}, error)

DefaultValue parse the DftValue to time.Time and if the DftValue == now time.Now() is returned

type TypeAdapter Uses

type TypeAdapter interface {
    DefaultValue(ctx context.Context, dftValue string) (interface{}, error)

TypeAdapter is an abstraction that define some behavior of target type usually, we don't use this to support basic type since golang has many restriction for basic types This is an important extension point

Package bean imports 7 packages (graph). Updated 2020-11-07. Refresh now. Tools for package owners.