1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-09 13:47:09 +02:00

Add proper database/go casing.

This commit is contained in:
Aaron 2015-01-18 14:35:44 -08:00
parent 934ddf5531
commit efc834006a
3 changed files with 71 additions and 21 deletions

View File

@ -1,6 +1,9 @@
package authboss
import "net/http"
import (
"errors"
"net/http"
)
// Context provides context for module operations and callbacks. One obvious
// need for context is a request's session store. It is not safe for use by
@ -96,7 +99,7 @@ func (c *Context) LoadUser(key string, storer Storer) error {
// SaveUser saves the user Attributes.
func (c *Context) SaveUser(key string, storer Storer) error {
if c.User == nil {
return nil
return errors.New("User not initialized.")
}
return storer.Put(key, c.User)

View File

@ -1,10 +1,10 @@
package authboss
import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"time"
"unicode"
)
@ -112,7 +112,7 @@ func (a Attributes) Bind(strct interface{}) error {
structType = structVal.Type()
for k, v := range a {
k = strings.Title(k)
k = underToCamel(k)
if _, has := structType.FieldByName(k); !has {
return fmt.Errorf("Bind: Struct was missing %s field, type: %v", k, reflect.TypeOf(v).String())
@ -165,7 +165,7 @@ func Unbind(intf interface{}) Attributes {
continue // Unexported
}
name = strings.ToLower(name)
name = camelToUnder(name)
switch field.Kind() {
case reflect.Struct:
@ -182,6 +182,39 @@ func Unbind(intf interface{}) Attributes {
return attr
}
func camelToUnder(in string) string {
out := bytes.Buffer{}
for i := 0; i < len(in); i++ {
chr := in[i]
if chr >= 'A' && chr <= 'Z' {
if i > 0 {
out.WriteByte('_')
}
out.WriteByte(chr + 'a' - 'A')
} else {
out.WriteByte(chr)
}
}
return out.String()
}
func underToCamel(in string) string {
out := bytes.Buffer{}
for i := 0; i < len(in); i++ {
chr := in[i]
if first := i == 0; first || chr == '_' {
if !first {
i++
}
out.WriteByte(in[i] - 'a' + 'A')
} else {
out.WriteByte(chr)
}
}
return out.String()
}
/*type postgresStorer struct {
*sql.DB
}

View File

@ -8,13 +8,13 @@ import (
func TestAttributes_Names(t *testing.T) {
attr := Attributes{
"integer": 5,
"string": "string",
"date": time.Now(),
"integer": 5,
"string": "string",
"date_time": time.Now(),
}
names := attr.Names()
found := map[string]bool{"integer": false, "string": false, "date": false}
found := map[string]bool{"integer": false, "string": false, "date_time": false}
for _, n := range names {
found[n] = true
}
@ -28,13 +28,13 @@ func TestAttributes_Names(t *testing.T) {
func TestAttributeMeta_Names(t *testing.T) {
meta := AttributeMeta{
"integer": Integer,
"string": String,
"date": DateTime,
"integer": Integer,
"string": String,
"date_time": DateTime,
}
names := meta.Names()
found := map[string]bool{"integer": false, "string": false, "date": false}
found := map[string]bool{"integer": false, "string": false, "date_time": false}
for _, n := range names {
found[n] = true
}
@ -64,15 +64,15 @@ func TestAttributes_Bind(t *testing.T) {
aTime := time.Now()
data := Attributes{
"Integer": anInteger,
"String": aString,
"Date": aTime,
"integer": anInteger,
"string": aString,
"date_time": aTime,
}
s := struct {
Integer int
String string
Date time.Time
Integer int
String string
DateTime time.Time
}{}
if err := data.Bind(&s); err != nil {
@ -85,8 +85,8 @@ func TestAttributes_Bind(t *testing.T) {
if s.String != aString {
t.Error("String was not set.")
}
if s.Date != aTime {
t.Error("Time was not set.")
if s.DateTime != aTime {
t.Error("DateTime was not set.")
}
}
@ -192,3 +192,17 @@ func TestAttributes_Unbind(t *testing.T) {
t.Error("Underlying value is wrong:", val)
}
}
func TestCasingStyleConversions(t *testing.T) {
camel := "SomethingInCamel"
got := camelToUnder(camel)
if got != "something_in_camel" {
t.Error(got)
}
got = underToCamel(got)
if got != camel {
t.Error(got)
}
}