Esempio n. 1
0
// Returns the default table name for a type
func defaultTableName(typ reflect.Type) string {
	n := typ.Name()
	if p := typ.PkgPath(); !strings.HasPrefix(p, "main") {
		n = strings.Replace(p, "/", "_", -1) + n
	}
	return stringutil.CamelCaseToLower(n, "_")
}
Esempio n. 2
0
// Register registers a new command with the
// given function and options.
func Register(f app.Handler, opts ...Option) error {
	o := options{}
	for _, f := range opts {
		o = f(o)
	}
	if o.Name == "" {
		qname := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
		p := strings.Split(qname, ".")
		o.Name = p[len(p)-1]
		if o.Name == "" {
			return fmt.Errorf("could not determine name for function %v. Please, provide a name using Name().", f)
		}
		o.Name = stringutil.CamelCaseToLower(o.Name, "-")
	}
	if _, ok := commands[o.Name]; ok {
		return fmt.Errorf("duplicate command name %q", o.Name)
	}
	commands[o.Name] = &command{
		handler: f,
		opts:    o,
	}
	return nil
}
Esempio n. 3
0
func (s *Struct) initializeFields(typ reflect.Type, qprefix, mprefix string, index []int) error {
	n := typ.NumField()
	for ii := 0; ii < n; ii++ {
		field := typ.Field(ii)
		if field.PkgPath != "" {
			// Unexported
			continue
		}
		ftag := NewTag(field, s.tags)
		name := ftag.Name()
		if name == "-" {
			// Ignored field
			continue
		}
		if name == "" {
			// Default name
			name = stringutil.CamelCaseToLower(field.Name, "_")
		}
		name = mprefix + name
		qname := qprefix + field.Name
		if prev, ok := s.MNameMap[name]; ok {
			return fmt.Errorf("duplicate field %q in %s: %s and %s", name, s.Type, s.QNames[prev], qname)
		}
		// Check type
		ptr := false
		t := field.Type
		for t.Kind() == reflect.Ptr {
			ptr = true
			t = t.Elem()
		}
		if t.Kind() == reflect.Struct && s.decomposeField(t, ftag) {
			// Inner struct
			idx := make([]int, len(index))
			copy(idx, index)
			idx = append(idx, field.Index[0])
			prefix := mprefix
			if !ftag.Has("inline") {
				prefix += name + "_"
			}
			err := s.initializeFields(t, qname+".", prefix, idx)
			if err != nil {
				return err
			}
			if ptr {
				s.Pointers = append(s.Pointers, idx)
			}
			continue
		}
		idx := make([]int, len(index))
		copy(idx, index)
		idx = append(idx, field.Index[0])
		s.MNames = append(s.MNames, name)
		s.QNames = append(s.QNames, qname)
		s.Indexes = append(s.Indexes, idx)
		s.Tags = append(s.Tags, ftag)
		s.Types = append(s.Types, t)
		p := len(s.MNames) - 1
		s.MNameMap[name] = p
		s.QNameMap[qname] = p
	}
	return nil
}
Esempio n. 4
0
func (f *Form) toHTMLName(name string) string {
	return stringutil.CamelCaseToLower(name, "_")
}
Esempio n. 5
0
func parameterName(name string) string {
	return stringutil.CamelCaseToLower(name, "-")
}