GithubHelp home page GithubHelp logo

initial's Introduction

initial

依赖注入初始化

使用

package initial_test

import (
	"testing"

	"github.com/Drelf2018/initial"
	"github.com/Drelf2018/initial/fullpath"
)

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

func (f *File) BeforeDefault() error {
	println("BeforeInitial:", f.Name)
	return nil
}

func (f File) AfterDefault() error {
	println("AfterInitial: ", f.Name)
	return nil
}

type Files []File

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

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

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

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

func (*Path) BeforePathFull(p *Path) (err error) {
	p.Full, err = fullpath.New(*p)
	if err != nil {
		return
	}
	return initial.ErrBreak
}

func (p *Path) BeforeDefault() error {
	p.Files = Files{{"default.go"}}
	return nil
}

func TestPath(t *testing.T) {
	result, err := initial.New[Path]()
	if err != nil {
		t.Fatal(err)
	}
	if result.Full.Version != `resource\views\.version` {
		t.Fail()
	}
}

控制台

=== RUN   TestPath
BeforeInitial: default.go
AfterInitial:  default.go
BeforeInitial:
AfterInitial:  initial.go
BeforeInitial: resource\public\posts.db
AfterInitial:  resource\public\posts.db
BeforeInitial: key
AfterInitial:  key
BeforeInitial: value
AfterInitial:  value
--- PASS: TestPath (0.00s)
PASS
ok      github.com/Drelf2018/initial    0.014s

initial's People

Contributors

drelf2018 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.