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.
29 lines
624 B
29 lines
624 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 {
|
|
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}
|
|
}
|