From 18220f5f3134fb26132d18fbefb6c0af773bb1f8 Mon Sep 17 00:00:00 2001 From: jacking Date: Wed, 19 Sep 2018 15:53:11 +0800 Subject: [PATCH] adding function to get column's value --- scope.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/scope.go b/scope.go index 378025bd..0b556757 100644 --- a/scope.go +++ b/scope.go @@ -238,6 +238,43 @@ func (scope *Scope) SetColumn(column interface{}, value interface{}) error { return errors.New("could not convert column to field") } +// GetColumn to get the column's value, column could be field or dbname +func (scope *Scope) GetColumn(column interface{}) (interface{}, error) { + + return scope.getColumn(column, false) +} + +// GetColumnByModelName to get column's value,column should be filed's name +func (scope *Scope) GetColumnByModelName(column string) (interface{}, error) { + + return scope.getColumn(column, true) +} + +// getColumn to get the column's value, column could be field or field's name/dbname +func (scope *Scope) getColumn(column interface{}, isModelName bool) (v interface{}, e error) { + + if field, ok := column.(*Field); ok { + + v = field.Field.Interface() + return + } + + for _, field := range *scope.fields { + + if isModelName && field.Name == column.(string) { + v = field.Field.Interface() + return + } + if !isModelName && field.DBName == column.(string) { + v = field.Field.Interface() + return + } + } + + e = errors.New("the field is not exist!") + return +} + // CallMethod call scope value's method, if it is a slice, will call its element's method one by one func (scope *Scope) CallMethod(methodName string) { if scope.Value == nil {