本文整理匯總了Golang中github.com/oleiade/reflections.SetField函數的典型用法代碼示例。如果您正苦於以下問題:Golang SetField函數的具體用法?Golang SetField怎麽用?Golang SetField使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SetField函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Bind
func (r *Row) Bind(x interface{}, strategy map[string]string) error {
for src, dest := range strategy {
data, ok := r.data[src]
if ok {
k, err := reflections.GetFieldKind(x, dest)
if err != nil {
return err
}
switch k {
case reflect.String:
{
reflections.SetField(x, dest, data)
}
case reflect.Int64:
{
i, err := strconv.ParseInt(data, 10, 64)
if err != nil {
return err
}
reflections.SetField(x, dest, i)
}
case reflect.Ptr:
{
value, err := reflections.GetField(x, dest)
if err != nil {
return err
}
if reflect.TypeOf(value) == typeDec {
dec, success := new(inf.Dec).SetString(data)
if !success {
fmt.Errorf("Could not parse inf.Dec: %s", data)
}
reflections.SetField(x, dest, dec)
}
}
case reflect.Struct:
{
value, err := reflections.GetField(x, dest)
if err != nil {
return err
}
if reflect.TypeOf(value) == typeTime {
date, err := time.ParseInLocation("2006-01-02 15:04:05", data, r.opts.TimeZone)
if err != nil {
return err
}
reflections.SetField(x, dest, date)
}
}
}
}
}
return nil
}
示例2: LoadModel
func (this *CCMomentHandler) LoadModel(moment models.CCMoment) models.Moment {
fields, _ := reflections.Fields(&models.Moment{})
//new instance
momentModel := models.Moment{}
for i := 0; i < len(fields); i++ {
has, _ := reflections.HasField(moment, fields[i])
if has == true {
fmt.Println("Field Exist------", fields[i])
if fields[i] == "Volunteer_id" {
fmt.Println("Volunteer_id Exist------")
var userObj CCUserHandler
var volunteerObj CCVolunteerHandler
var usermodel models.CCUser
var volunteerModel models.CCVolunteer
value, err := reflections.GetField(moment, fields[i])
if err == nil && value != nil {
str, _ := value.(string)
volunteerModel, err = volunteerObj.FetchVolunteerForId(str)
if err == nil {
fmt.Println("volunteer Exist------", volunteerModel)
usermodel, err = userObj.FetchUserById(volunteerModel.User_id)
fmt.Println("User Exist------", usermodel)
if err == nil {
responseModel := userObj.LoadModel(usermodel)
setError := reflections.SetField(&momentModel, fields[i], responseModel) // err != nil
if setError != nil {
panic(setError)
}
}
}
}
} else {
value, err := reflections.GetField(moment, fields[i])
if err == nil {
fmt.Println("Field Value------", value)
setError := reflections.SetField(&momentModel, fields[i], value) // err != nil
if setError != nil {
panic(setError)
}
}
}
}
}
fmt.Println("Data set to Model --- ", momentModel)
return momentModel
}
示例3: LoadModel
func (this *CCUserHandler) LoadModel(user models.CCUser) models.User {
fields, _ := reflections.Fields(&models.User{})
//new instance
userModel := models.User{}
for i := 0; i < len(fields); i++ {
has, _ := reflections.HasField(user, fields[i])
if has == true {
fmt.Println("Field Exist------", fields[i])
value, err := reflections.GetField(user, fields[i])
if err == nil {
fmt.Println("Field Value------", value)
setError := reflections.SetField(&userModel, fields[i], value) // err != nil
if setError != nil {
panic(setError)
}
}
}
}
fmt.Println("Data set to Model --- ", userModel)
return userModel
}
示例4: LoadModel
func (this *CCEventHandler) LoadModel(event models.CCEvent) models.Event {
fields, _ := reflections.Fields(&models.Event{})
//new instance
eventModel := models.Event{}
for i := 0; i < len(fields); i++ {
has, _ := reflections.HasField(event, fields[i])
if has == true {
fmt.Println("Field Exist------", fields[i])
if fields[i] == "Created_by" {
var userObj CCUserHandler
var usermodel models.CCUser
value, err := reflections.GetField(event, fields[i])
if err == nil && value != nil {
str, _ := value.(string)
usermodel, err = userObj.FetchUserById(str)
if err == nil {
responseModel := userObj.LoadModel(usermodel)
setError := reflections.SetField(&eventModel, fields[i], responseModel) // err != nil
if setError != nil {
panic(setError)
}
}
}
} else {
value, err := reflections.GetField(event, fields[i])
if err == nil {
fmt.Println("Field Value------", value)
setError := reflections.SetField(&eventModel, fields[i], value) // err != nil
if setError != nil {
panic(setError)
}
}
}
}
}
fmt.Println("Data set to Model --- ", eventModel)
return eventModel
}
示例5: PopulateHoursByDays
func (D *DayOfWeekCalls) PopulateHoursByDays(results []HourByDaysRecord) {
for i := 0; i < len(results); i++ {
hourName := fmt.Sprintf("H%d", i)
dayName := fmt.Sprintf("Day%d", D.DayOfWeek-1)
valueHour, _ := reflections.GetField(D.SummaryCallsPerHours, hourName)
_ = reflections.SetField(&results[i], dayName, valueHour)
}
}
示例6: ExampleSetField
func ExampleSetField() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
// In order to be able to set the structure's values,
// a pointer to it has to be passed to it.
err := reflections.SetField(&s, "FirstField", "new value")
if err != nil {
log.Fatal(err)
}
// If you try to set a field's value using the wrong type,
// an error will be returned
err = reflections.SetField(&s, "FirstField", 123) // err != nil
if err != nil {
log.Fatal(err)
}
}
示例7: hookDisableWebpage
func hookDisableWebpage(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "DisableWebPagePreview")
if has {
value, _ := reflections.GetField(payload, "DisableWebPagePreview")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "DisableWebPagePreview", nv)
}
}
}
}
示例8: hookReplyToMessageID
func hookReplyToMessageID(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "ReplyToMessageID")
if has {
value, _ := reflections.GetField(payload, "ReplyToMessageID")
bvalue := value.(*int)
if bvalue == nil {
reflections.SetField(payload, "ReplyToMessageID", nv)
}
}
}
}
示例9: hookSelective
func hookSelective(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "Selective")
if has {
value, _ := reflections.GetField(payload, "Selective")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "Selective", nv)
}
}
}
}
示例10: hookOneTimeKeyboard
func hookOneTimeKeyboard(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "OneTimeKeyboard")
if has {
value, _ := reflections.GetField(payload, "OneTimeKeyboard")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "OneTimeKeyboard", nv)
}
}
}
}
示例11: HandleStructField
// HandleStructField sets the value for the given supported struct field.
func (dc *DeepCopier) HandleStructField(options *FieldOptions) error {
f, err := reflections.GetField(dc.Source, options.SourceField)
if err != nil {
return err
}
switch v := f.(type) {
case pq.NullTime:
if v.Valid {
if err := reflections.SetField(dc.Destination, options.DestinationField, &v.Time); err != nil {
return err
}
}
case time.Time:
if err := reflections.SetField(dc.Destination, options.DestinationField, v); err != nil {
return err
}
}
return nil
}
示例12: UpdateUser
func (s *UserStore) UpdateUser(user *User, changes map[string]interface{}) (err error) {
u := s.ByName(user.Username)
u.mux.Lock()
for k, v := range changes {
err = reflections.SetField(u, k, v)
}
u.mux.Unlock()
if err != nil {
return err
}
s.save()
return nil
}
示例13: setProperty
func setProperty(obj interface{}, prop string, val interface{}) error {
if reflect.TypeOf(obj).Kind() == reflect.Map {
value := reflect.ValueOf(obj)
value.SetMapIndex(reflect.ValueOf(prop), reflect.ValueOf(val))
return nil
}
if reflect.TypeOf(obj).Kind() != reflect.Ptr {
return errors.New("Object must be a pointer to a struct")
}
prop = strings.Title(prop)
return reflections.SetField(obj, prop, val)
}
示例14: OverrideWith
// OverrideWith will override the Environment values
// according to a provided key-value map.
func (e *Environment) OverrideWith(data map[string]string) error {
for field, value := range data {
has, err := reflections.HasField(*e, field)
if !has {
errMsg := fmt.Sprintf("No such field %s in Environement struct",
field)
return errors.New(errMsg)
} else if err != nil {
return err
}
if value != "" {
reflections.SetField(e, field, value)
}
}
return nil
}
示例15: SetDefaults
func (d *Dsn) SetDefaults(defaults map[string]string) error {
for k, v := range defaults {
// Check the dsn struct has field
value, err := reflections.GetField(d, k)
if err != nil {
return err
}
// If dsn instance field is set to zero value,
// then override it with provided default value
if value == "" {
err := reflections.SetField(d, k, v)
if err != nil {
return err
}
}
}
return nil
}