bean

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const DefaultValueTagKey = "default"

Variables

This section is empty.

Functions

func Mock

func Mock(v interface{}) (err error)

the mock object must be pointer of struct the element in mock object can be slices, structures, basic data types, pointers and interface

Types

type ApplicationContext

type ApplicationContext interface{}

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

type AutoWireBeanFactory

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

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

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

FieldMetadata contains metadata

type TagAutoWireBeanFactory

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

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

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

type TimeTypeAdapter struct {
	Layout string
}

TimeTypeAdapter process the time.Time

func (*TimeTypeAdapter) DefaultValue

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

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

Jump to

Keyboard shortcuts

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