You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
905 B
38 lines
905 B
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 {
|
|
switch v := object.(type) {
|
|
case models.Post:
|
|
return models.ChangeKey{Model: "Post", ID: v.ID}
|
|
case models.Character:
|
|
return models.ChangeKey{Model: "Character", ID: v.ID}
|
|
case models.Channel:
|
|
return models.ChangeKey{Model: "Channel", ID: v.Name}
|
|
}
|
|
|
|
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: models.ChangeModel(model), ID: id}
|
|
}
|