예제 #1
0
파일: validate.go 프로젝트: kidstuff/toys
func Valid(i interface{}) (*Invalid, error) {
	v := reflect.ValueOf(i)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	if v.Kind() != reflect.Struct {
		return nil, errs.New("validate: function must recieve a struct or a pointer to struct")
	}
}
예제 #2
0
파일: model.go 프로젝트: kidstuff/toys
// Load return a Driver with name. An error return if the driver not exist
func Load(name string) (Driver, error) {
	mux.Lock()
	defer mux.Unlock()

	driver, ok := drivers[name]
	if !ok {
		return nil, errs.New("model: driver " + name + " not exist")
	}
	return driver, nil
}
예제 #3
0
파일: locale.go 프로젝트: kidstuff/toys
// Parse parses the lang-set and cache them
func (l *Lang) Parse(set string) error {
	setFolder := filepath.Join(l.root, set)

	setroot, err := os.Open(setFolder)
	if err != nil {
		return errs.New("lang: cannot open language set folder")
	}
	defer setroot.Close()

	files, err := setroot.Readdir(-1)
	if err != nil {
		return errs.New("lang: cannot list file in language set folder")
	}

	l.set[set] = make(map[string]map[string]string)
	for _, file := range files {
		if !file.IsDir() {
			//read file
			f, err := os.Open(filepath.Join(setFolder, file.Name()))
			if err != nil {
				continue
			}
			defer f.Close()

			r := bufio.NewReader(f)
			s, e := readln(r)
			if e != nil {
				continue
			}

			m := make(map[string]string)
			for e == nil {
				pos := bytes.Index(s, []byte{0x3d})
				m[string(s[:pos])] = string(s[pos+1:])
				s, e = readln(r)
			}
			l.set[set][file.Name()] = m
		}
	}
	l.current = set
	return nil
}
예제 #4
0
파일: confg.go 프로젝트: kidstuff/toys
import (
	"github.com/kidstuff/toys/util/errs"
)

type Configurator interface {
	Load(path string) error
	Close() error
	Set(k string, v interface{})
	Get(k string) interface{}
	Del(k string)
}

var configurators = make(map[string]Configurator)

var (
	ErrConfiguratorNotFound = errs.New("confg: Configurator not found")
)

func Register(name string, configurator Configurator) {
	if configurator == nil {
		panic("confg: Register configurator is nil")
	}

	if _, dup := configurators[name]; dup {
		panic("confg: Register called twice for " + name)
	}

	configurators[name] = configurator
}

func Open(name, path string) (Configurator, error) {