func ExampleHasField() {
	s := MyStruct{
		FirstField:  "first value",
		SecondField: 2,
		ThirdField:  "third value",
	}

	// has == true
	has, _ := reflections.HasField(s, "FirstField")
	fmt.Println(has)

	// has == false
	has, _ = reflections.HasField(s, "FourthField")
	fmt.Println(has)
}
Beispiel #2
0
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
}
Beispiel #3
0
// GetNextPage provided a collection of resources (Builds or Jobs),
// will update the ListOptions to fetch the next resource page on next call.
func (into *ListOptions) GetNextPage(from interface{}) error {
	if reflect.TypeOf(from).Kind() != reflect.Slice {
		return fmt.Errorf("provided interface{} does not represent a slice")
	}

	slice := reflect.ValueOf(from)
	if slice.Len() == 0 {
		return fmt.Errorf("provided interface{} is a zero sized slice")
	}

	lastElem := slice.Index(slice.Len() - 1).Interface()
	has, _ := reflections.HasField(lastElem, "Number")
	if !has {
		return fmt.Errorf("last element of the provided slice does not have a Number attribute")
	}

	value, err := reflections.GetField(lastElem, "Number")
	if err != nil {
		return err
	}

	// We rely on travis sending us numbers representations here
	// so no real need to check for errors
	number, _ := strconv.ParseUint(value.(string), 10, 64)
	into.AfterNumber = uint(math.Max(float64(number), 0))

	return nil
}
Beispiel #4
0
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
}
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)
			}
		}
	}
}
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)
			}
		}
	}
}
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)
			}
		}
	}
}
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)
			}
		}
	}
}
Beispiel #9
0
// SetField sets the value of the given field.
func (dc *DeepCopier) SetField(options *FieldOptions) error {
	if options.Skip {
		return nil
	}

	if dc.Reversed {
		has, _ := reflections.HasField(dc.Destination, options.SourceField)
		if !has {
			return nil
		}
	}

	has, _ := reflections.HasField(dc.Source, options.SourceField)
	if !has {
		err := dc.HandleMethod(options)
		if err != nil {
			has, _ = reflections.HasField(dc.Destination, options.DestinationField)
			if has {
				return nil
			}
		}
		return nil
	}

	kind, _ := reflections.GetFieldKind(dc.Source, options.SourceField)
	if kind == reflect.Struct {
		if err := dc.HandleStructField(options); err != nil {
			return err
		}
		return nil
	}

	if err := dc.HandleField(options); err != nil {
		return err
	}

	return nil
}
Beispiel #10
0
// 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
}
Beispiel #11
0
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
}
// HookPayload I hate reflection, sorry for that <3
func hookPayload(payload interface{}, opts DefaultOptionsBot) {
	hookDisableWebpage(payload, opts.DisableWebURL)
	// HookReplyToMessageID(payload, opts.ReplyToMessageID)

	has, _ := reflections.HasField(payload, "ReplyMarkup")

	if has {
		keyint, _ := reflections.GetField(payload, "ReplyMarkup")

		switch val := keyint.(type) {
		case *ForceReply, *ReplyKeyboardHide:
			if val != nil {
				hookSelective(val, opts.Selective)
			}
		case *ReplyKeyboardMarkup:
			if val != nil {
				hookOneTimeKeyboard(val, opts.OneTimeKeyboard)
				hookSelective(val, opts.Selective)
			}
		default:
		}
	}
}