initial

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 7 Imported by: 2

README

initial

依赖注入初始化

使用
package initial_test

import (
	"errors"
	"reflect"
	"testing"

	"github.com/Drelf2018/initial"
)

type File struct {
	Name string `default:"initial.go"`
}

func (f *File) BeforeInitial() {
	println("FileBeforeInitial:", f.Name)
}

func (f File) AfterInitial() error {
	if f.Name == "error.log" {
		return errors.New("test error")
	}
	println("FileAfterInitial: ", f.Name)
	return nil
}

type Files []File

type Path struct {
	ID uint16 `default:"9000"`

	Root    string `default:"resource"`
	Views   string `default:"views"      join:"Root"`
	Public  string `default:"public"     join:"Root"`
	Posts   string `default:"posts.db"   join:"Public"`
	Users   string `default:"users.db"   join:"Root"`
	Log     string `default:".log"       join:"Root"`
	Index   string `default:"index.html" join:"Views"`
	Version string `default:".version"   join:"Views"`
	Full    *Path

	Data struct {
		D1 string  `default:"d1"`
		D2 bool    `default:"true"`
		D3 float64 `default:"3.14"`
		D4 int64   `default:"114"`
	}

	Files   Files
	FileMap []map[*File]*File

	// Error error `default:"some error"` // can't work
	Error error `default:"$myError"` // works
}

func init() {
	initial.SetDefaultValue("$myError", errors.New("some error"))
}

func (p *Path) BeforeFiles() {
	p.Files = append(p.Files, File{}, File{p.Full.Posts})
	p.FileMap = []map[*File]*File{{{"key.txt"}: {"value.txt"}}}
}

func (p *Path) BeforeFull() {
	p.Full.Posts = p.Posts
}

func (p *Path) BeforeInitial() {
	p.Files = append(p.Files, File{"default.go"})
}
测试解析结果
func TestParse(t *testing.T) {
	values := initial.ParseValues(reflect.TypeOf(Path{}))
	for _, val := range values {
		t.Log(val)
	}
}
initial_test.go:76: {0 true <uint16 Value> <invalid Value> <invalid Value>}
initial_test.go:76: {1 true resource <invalid Value> <invalid Value>}
initial_test.go:76: {2 true views <invalid Value> <invalid Value>}
initial_test.go:76: {3 true public <invalid Value> <invalid Value>}
initial_test.go:76: {4 true posts.db <invalid Value> <invalid Value>}
initial_test.go:76: {5 true users.db <invalid Value> <invalid Value>}
initial_test.go:76: {6 true .log <invalid Value> <invalid Value>}
initial_test.go:76: {7 true index.html <invalid Value> <invalid Value>}
initial_test.go:76: {8 true .version <invalid Value> <invalid Value>}
initial_test.go:76: {9 false <invalid Value> <func(*initial_test.Path) Value> <invalid Value>}
initial_test.go:76: {10 true <invalid Value> <invalid Value> <invalid Value>}
initial_test.go:76: {11 true <invalid Value> <func(*initial_test.Path) Value> <invalid Value>}
initial_test.go:76: {12 true <invalid Value> <invalid Value> <invalid Value>}
initial_test.go:76: {13 true <*errors.errorString Value> <invalid Value> <invalid Value>}
测试初始化
func TestInitial(t *testing.T) {
	result, err := initial.New[Path]()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(result)
}
FileBeforeInitial: default.go
FileAfterInitial:  default.go
FileBeforeInitial:
FileAfterInitial:  initial.go
FileBeforeInitial: posts.db
FileAfterInitial:  posts.db
FileBeforeInitial: key.txt
FileAfterInitial:  key.txt
FileBeforeInitial: value.txt
FileAfterInitial:  value.txt
    initial_test.go:85: &{9000 resource views public posts.db users.db .log index.html .version 0xc000170200 {d1 true 3.14 114} [{default.go} {initial.go} {posts.db}] [map[0xc00010e800:0xc00010e7f0]] some error}
测试运行时返回错误
func TestError(t *testing.T) {
	err := initial.Initial(&Path{Files: []File{{"error.log"}}})
	if err == nil {
		t.Fatal()
	}
	t.Log(err)
}
FileBeforeInitial: error.log
    initial_test.go:93: test error

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidValue         = errors.New("initial: invalid value")
	ErrNotStruct            = errors.New("initial: the value must be a struct")
	ErrNotAddressableStruct = errors.New("initial: the struct value must be addressable")
)

Functions

func Call added in v0.9.0

func Call(fn, parent reflect.Value) error

func Indirect added in v0.8.0

func Indirect(t reflect.Type) reflect.Type

func Initial added in v0.9.0

func Initial(v any) error

func InitialStruct added in v0.9.0

func InitialStruct(v reflect.Value) error

func InitialValue added in v0.9.0

func InitialValue(v reflect.Value) error

func IsOrdinaryValue added in v0.8.0

func IsOrdinaryValue(fieldType reflect.Type) bool

func IsRecursiveType added in v0.9.0

func IsRecursiveType(typ reflect.Type) bool

func MustNew added in v0.8.0

func MustNew[T any]() *T

func New added in v0.8.0

func New[T any]() (*T, error)

func ParseOrdinaryValue added in v0.8.0

func ParseOrdinaryValue(fieldType reflect.Type, val string) (v reflect.Value, err error)

ParseOrdinaryValue convert val to reflect.Value with type fieldType.

If the usual Go conversion rules do not allow this conversion, ParseOrdinaryValue returns error.

func SetDefaultValue added in v0.8.0

func SetDefaultValue(name string, value any)

func TypePtr added in v0.9.0

func TypePtr(in any) uintptr

func ValuePtr added in v0.9.0

func ValuePtr(in any) uintptr

ValuePtr can obtain the uintptr of a type from its reflect.Type

TypePtr(something{}) is equal to ValuePtr(reflect.TypeOf(something{}))

Types

type AfterInitial1 added in v0.9.0

type AfterInitial1 interface {
	AfterInitial()
}

type AfterInitial2 added in v0.9.0

type AfterInitial2 interface {
	AfterInitial() error
}

type Any added in v0.9.0

type Any struct {
	Type  unsafe.Pointer
	Value unsafe.Pointer
}

type BeforeInitial1 added in v0.9.0

type BeforeInitial1 interface {
	BeforeInitial()
}

type BeforeInitial2 added in v0.9.0

type BeforeInitial2 interface {
	BeforeInitial() error
}

type Value added in v0.8.0

type Value struct {
	Index   int
	Initial bool
	Default reflect.Value
	Before  reflect.Value
	After   reflect.Value
}

func Load added in v0.9.0

func Load(in reflect.Type) []Value

func ParseValues added in v0.9.0

func ParseValues(parent reflect.Type) (values []Value)

Jump to

Keyboard shortcuts

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