Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement positive number tag #388

Open
hazcod opened this issue Jun 22, 2020 · 2 comments · May be fixed by #453
Open

Implement positive number tag #388

hazcod opened this issue Jun 22, 2020 · 2 comments · May be fixed by #453

Comments

@hazcod
Copy link

hazcod commented Jun 22, 2020

Replacement struct tag for IsPositive.

@hazcod
Copy link
Author

hazcod commented Jun 25, 2020

Possibly using:

func isPositiveNumber(v interface{}, _ interface{}) bool {
	if v == nil { return false }

	switch v.(type) {
	case []uint:
		for _, vv := range v.([]uint) {
			if !isPositiveNumber(vv, nil) { return false }
		}
		return true
	case []int:
		for _, vv := range v.([]int) {
			if !isPositiveNumber(vv, nil) { return false }
		}
		return true
	case int:
		break
	case int16:
		break
	case int32:
		break
	case int64:
		break
	case uint:
		break
	case uint16:
		break
	case uint32:
		break
	case uint64:
		break
	case float32:
		break
	case float64:
		break
	default:
		logrus.Warnf("field is not type numeric: '%s'", v)
		return false
	}

	if i, ok := v.(uint); ok {
		return i > 0
	}

	if i, ok := v.(int); ok {
		return i > 0
	}

	if i, ok := v.(int64); ok {
		return i > 0
	}

	if i, ok := v.(int32); ok {
		return i > 0
	}

	if i, ok := v.(uint32); ok {
		return i > 0
	}

	if i, ok := v.(uint64); ok {
		return i > 0
	}

	if i, ok := v.(float64); ok {
		return i > 0
	}

	if i, ok := v.(float32); ok {
		return i > 0
	}

	logrus.Errorf("unsupported type for positive check: %+v (%s)", v, reflect.TypeOf(v).Kind().String())
	return false
}

@Jesse0Michael Jesse0Michael linked a pull request Oct 6, 2021 that will close this issue
@sergeyglazyrindev
Copy link

Hello guys!
I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back
Link to my repo: create issue there and we'll discuss it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants