Browse Source
models: Changed change logging system to be allow for better querying in the future.
1.1
models: Changed change logging system to be allow for better querying in the future.
1.1
Gisle Aune
6 years ago
13 changed files with 224 additions and 25 deletions
-
5graph2/queries/channel.go
-
11graph2/queries/character.go
-
7graph2/queries/log.go
-
27graph2/queries/post.go
-
18models/change.go
-
8models/changekeys/all.go
-
10models/changekeys/listed.go
-
27models/changekeys/many.go
-
52models/changekeys/many_test.go
-
29models/changekeys/one.go
-
42models/changekeys/one_test.go
-
1models/changes/db.go
-
12models/changes/submit.go
@ -0,0 +1,8 @@ |
|||||
|
package changekeys |
||||
|
|
||||
|
import "git.aiterp.net/rpdata/api/models" |
||||
|
|
||||
|
// All makes a changelog that's (model, "*"). It's a helper to standardize it.
|
||||
|
func All(model string) models.ChangeKey { |
||||
|
return models.ChangeKey{Model: model, ID: "*"} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package changekeys |
||||
|
|
||||
|
import "git.aiterp.net/rpdata/api/models" |
||||
|
|
||||
|
// Listed is a helper for cases like []models.ChangeKey{changekeys.All("Logs"), changekeys.One(log)}
|
||||
|
func Listed(object interface{}) []models.ChangeKey { |
||||
|
key := One(object) |
||||
|
|
||||
|
return []models.ChangeKey{key, All(key.Model)} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package changekeys |
||||
|
|
||||
|
import ( |
||||
|
"reflect" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
// Many returns a key set matching the input objects, but without the asterisk key. You may pass
|
||||
|
// a slice as an argument, for example (log, posts) and it will be flattened
|
||||
|
func Many(objects ...interface{}) []models.ChangeKey { |
||||
|
keys := make([]models.ChangeKey, 0, len(objects)) |
||||
|
for _, object := range objects { |
||||
|
if v := reflect.ValueOf(object); v.Kind() == reflect.Slice { |
||||
|
elems := make([]interface{}, 0, v.Len()) |
||||
|
for i := 0; i < v.Len(); i++ { |
||||
|
elems = append(elems, v.Index(i).Interface()) |
||||
|
} |
||||
|
|
||||
|
keys = append(keys, Many(elems...)...) |
||||
|
} else { |
||||
|
keys = append(keys, One(object)) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return keys |
||||
|
} |
@ -0,0 +1,52 @@ |
|||||
|
package changekeys_test |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models/changekeys" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
func TestMany(t *testing.T) { |
||||
|
data := []interface{}{ |
||||
|
models.Log{ID: "Stuff"}, |
||||
|
[]interface{}{ |
||||
|
models.Post{ID: "P1"}, |
||||
|
models.Post{ID: "P2"}, |
||||
|
models.Post{ID: "P3"}, |
||||
|
[]interface{}{ |
||||
|
models.Post{ID: "P4"}, |
||||
|
models.Character{ID: "C17"}, |
||||
|
models.Channel{Name: "#Stuff"}, |
||||
|
}, |
||||
|
}, |
||||
|
models.Post{ID: "P5"}, |
||||
|
} |
||||
|
|
||||
|
expectations := []models.ChangeKey{ |
||||
|
{Model: "Log", ID: "Stuff"}, |
||||
|
{Model: "Post", ID: "P1"}, |
||||
|
{Model: "Post", ID: "P2"}, |
||||
|
{Model: "Post", ID: "P3"}, |
||||
|
{Model: "Post", ID: "P4"}, |
||||
|
{Model: "Character", ID: "C17"}, |
||||
|
{Model: "Channel", ID: "#Stuff"}, |
||||
|
{Model: "Post", ID: "P5"}, |
||||
|
} |
||||
|
|
||||
|
results := changekeys.Many(data...) |
||||
|
|
||||
|
if len(results) != len(expectations) { |
||||
|
t.Fatal("Incorrect result length") |
||||
|
} |
||||
|
|
||||
|
for i := range expectations { |
||||
|
result := results[i] |
||||
|
expectation := expectations[i] |
||||
|
|
||||
|
if result.ID != expectation.ID || result.Model != expectation.Model { |
||||
|
t.Errorf("Incorrect: actual(%#+v, %#+v) != expected(%#+v, %#+v)", result.Model, result.ID, expectation.Model, expectation.ID) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package changekeys |
||||
|
|
||||
|
import ( |
||||
|
"reflect" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
// One makes a ChangeKey for a model, or panics if it's not supported.
|
||||
|
func One(object interface{}) models.ChangeKey { |
||||
|
model := "" |
||||
|
if t := reflect.TypeOf(object); t.Kind() == reflect.Ptr { |
||||
|
model = t.Elem().Name() |
||||
|
} else { |
||||
|
model = t.Name() |
||||
|
} |
||||
|
|
||||
|
id := "" |
||||
|
v := reflect.ValueOf(object) |
||||
|
if f := v.FieldByName("ID"); f.Kind() == reflect.String { |
||||
|
id = f.String() |
||||
|
} else if f = v.FieldByName("Name"); f.Kind() == reflect.String { |
||||
|
id = f.String() |
||||
|
} else { |
||||
|
panic("Unsupported model") |
||||
|
} |
||||
|
|
||||
|
return models.ChangeKey{Model: model, ID: id} |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
package changekeys_test |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
"git.aiterp.net/rpdata/api/models/changekeys" |
||||
|
) |
||||
|
|
||||
|
func TestOne(t *testing.T) { |
||||
|
table := []struct { |
||||
|
Label string |
||||
|
Input interface{} |
||||
|
Output models.ChangeKey |
||||
|
}{ |
||||
|
{ |
||||
|
"Character_C17", |
||||
|
models.Character{ID: "C17"}, |
||||
|
models.ChangeKey{Model: "Character", ID: "C17"}, |
||||
|
}, |
||||
|
{ |
||||
|
"Channel_#Miner'sRespite", |
||||
|
models.Channel{Name: "#Miner'sRespite"}, |
||||
|
models.ChangeKey{Model: "Channel", ID: "#Miner'sRespite"}, |
||||
|
}, |
||||
|
{ |
||||
|
"Log_2018-10-23_210303325_RedrockAgency", |
||||
|
models.Log{ID: "2018-10-23_210303325_RedrockAgency", ShortID: "L807"}, |
||||
|
models.ChangeKey{Model: "Log", ID: "2018-10-23_210303325_RedrockAgency"}, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
for _, row := range table { |
||||
|
t.Run(row.Label, func(t *testing.T) { |
||||
|
key := changekeys.One(row.Input) |
||||
|
|
||||
|
if key.ID != row.Output.ID || key.Model != row.Output.Model { |
||||
|
t.Errorf("Incorrect: actual(%#+v, %#+v) != expected(%#+v, %#+v)", key.Model, key.ID, row.Output.Model, row.Output.ID) |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue