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} }