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:
parent
934ddf5531
commit
efc834006a
@ -1,6 +1,9 @@
|
|||||||
package authboss
|
package authboss
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
// Context provides context for module operations and callbacks. One obvious
|
// 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
|
// 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.
|
// SaveUser saves the user Attributes.
|
||||||
func (c *Context) SaveUser(key string, storer Storer) error {
|
func (c *Context) SaveUser(key string, storer Storer) error {
|
||||||
if c.User == nil {
|
if c.User == nil {
|
||||||
return nil
|
return errors.New("User not initialized.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return storer.Put(key, c.User)
|
return storer.Put(key, c.User)
|
||||||
|
39
storer.go
39
storer.go
@ -1,10 +1,10 @@
|
|||||||
package authboss
|
package authboss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
@ -112,7 +112,7 @@ func (a Attributes) Bind(strct interface{}) error {
|
|||||||
structType = structVal.Type()
|
structType = structVal.Type()
|
||||||
for k, v := range a {
|
for k, v := range a {
|
||||||
|
|
||||||
k = strings.Title(k)
|
k = underToCamel(k)
|
||||||
|
|
||||||
if _, has := structType.FieldByName(k); !has {
|
if _, has := structType.FieldByName(k); !has {
|
||||||
return fmt.Errorf("Bind: Struct was missing %s field, type: %v", k, reflect.TypeOf(v).String())
|
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
|
continue // Unexported
|
||||||
}
|
}
|
||||||
|
|
||||||
name = strings.ToLower(name)
|
name = camelToUnder(name)
|
||||||
|
|
||||||
switch field.Kind() {
|
switch field.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@ -182,6 +182,39 @@ func Unbind(intf interface{}) Attributes {
|
|||||||
return attr
|
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 {
|
/*type postgresStorer struct {
|
||||||
*sql.DB
|
*sql.DB
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,13 @@ import (
|
|||||||
|
|
||||||
func TestAttributes_Names(t *testing.T) {
|
func TestAttributes_Names(t *testing.T) {
|
||||||
attr := Attributes{
|
attr := Attributes{
|
||||||
"integer": 5,
|
"integer": 5,
|
||||||
"string": "string",
|
"string": "string",
|
||||||
"date": time.Now(),
|
"date_time": time.Now(),
|
||||||
}
|
}
|
||||||
names := attr.Names()
|
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 {
|
for _, n := range names {
|
||||||
found[n] = true
|
found[n] = true
|
||||||
}
|
}
|
||||||
@ -28,13 +28,13 @@ func TestAttributes_Names(t *testing.T) {
|
|||||||
|
|
||||||
func TestAttributeMeta_Names(t *testing.T) {
|
func TestAttributeMeta_Names(t *testing.T) {
|
||||||
meta := AttributeMeta{
|
meta := AttributeMeta{
|
||||||
"integer": Integer,
|
"integer": Integer,
|
||||||
"string": String,
|
"string": String,
|
||||||
"date": DateTime,
|
"date_time": DateTime,
|
||||||
}
|
}
|
||||||
names := meta.Names()
|
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 {
|
for _, n := range names {
|
||||||
found[n] = true
|
found[n] = true
|
||||||
}
|
}
|
||||||
@ -64,15 +64,15 @@ func TestAttributes_Bind(t *testing.T) {
|
|||||||
aTime := time.Now()
|
aTime := time.Now()
|
||||||
|
|
||||||
data := Attributes{
|
data := Attributes{
|
||||||
"Integer": anInteger,
|
"integer": anInteger,
|
||||||
"String": aString,
|
"string": aString,
|
||||||
"Date": aTime,
|
"date_time": aTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
s := struct {
|
s := struct {
|
||||||
Integer int
|
Integer int
|
||||||
String string
|
String string
|
||||||
Date time.Time
|
DateTime time.Time
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
if err := data.Bind(&s); err != nil {
|
if err := data.Bind(&s); err != nil {
|
||||||
@ -85,8 +85,8 @@ func TestAttributes_Bind(t *testing.T) {
|
|||||||
if s.String != aString {
|
if s.String != aString {
|
||||||
t.Error("String was not set.")
|
t.Error("String was not set.")
|
||||||
}
|
}
|
||||||
if s.Date != aTime {
|
if s.DateTime != aTime {
|
||||||
t.Error("Time was not set.")
|
t.Error("DateTime was not set.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,3 +192,17 @@ func TestAttributes_Unbind(t *testing.T) {
|
|||||||
t.Error("Underlying value is wrong:", val)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user