Example #1
0
func (in *C) Index(f func(v interface{}) string) *I {
	var (
		out = &I{elem_type: in.elem_type, tx: in.tx}
	)

	out.t.Do(func() error {
		if err := in.t.Wait(); err != nil {
			return err
		}

		var (
			mapped = make(map[string]interface{}, len(in.elems))
		)

		for _, elem := range in.elems {
			key := f(elem)
			if _, p := mapped[key]; p {
				return errors.Fmt("duplicate key: %s", key)
			}
			mapped[key] = elem
		}

		out.elems = mapped
		return nil
	})

	return out
}
Example #2
0
func (f Factory) build(c *container_t) error {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	app := &Application{
		container: c,
		dynamic:   mux.NewRouter(),
	}

	f(app)

	if app.Name == "" {
		return errors.Fmt("application name must not be empty.")
	}

	if _, p := c.app_map[app.Name]; p {
		return errors.Fmt("%s: application name must be unique.", app.Name)
	}

	if app.Generator == nil {
		return errors.Fmt("%s: application must have a generator.", app.Name)
	}

	app.database = c.database
	app.src = store.SubStore(c.src, app.Name)
	app.dst = store.SubStore(c.dst, app.Name)

	static, err := shttp.NewRouteHandler(store.Cache(app.dst))
	if err != nil {
		return errors.Forward(err, "%s: error while loading the route handler.", app.Name)
	}

	app.static = static
	app.dynamic.NotFoundHandler = static

	c.router.Add(app)
	c.apps = append(c.apps, app)
	c.app_map[app.Name] = app

	return nil
}
Example #3
0
func OpenOld(source string) (Store, error) {
	u, err := url.Parse(source)
	if err != nil {
		return nil, err
	}

	factory, p := registry[u.Scheme]
	if !p {
		return nil, errors.Fmt("Unknown store type: %s", u.Scheme)
	}

	return factory(u)
}
Example #4
0
func (r *route_set_writer) add(rule *route_rule) error {
	r.mtx.Lock()
	defer r.mtx.Unlock()

	for _, other := range r.Rules {
		if compare_lookup(other, rule) == 0 {

			if conflict(other, rule) {
				return errors.Fmt("Conflicting rule: %+v", rule)
			} else {
				return nil
			}

		}
	}

	r.Rules = append(r.Rules, rule)
	return nil
}