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

Map keys/values do not support in/not_in rules #740

Closed
haines opened this issue Nov 24, 2022 · 1 comment
Closed

Map keys/values do not support in/not_in rules #740

haines opened this issue Nov 24, 2022 · 1 comment

Comments

@haines
Copy link

haines commented Nov 24, 2022

The generated code for in/not_in rules for map keys/values does not include the lookup tables.

A failing test case is here: haines@41fe774

This generates invalid Go code (the same applies for C++):

message MapKeysValuesIn { map<string, string> val = 1 [(validate.rules).map = {keys: {string: {in: ["foo", "bar"]}}, values: {string: {in: ["baz", "qux"]}}}]; }
func (m *MapKeysValuesIn) validate(all bool) error {
	if m == nil {
		return nil
	}

	var errors []error

	{
		sorted_keys := make([]string, len(m.GetVal()))
		i := 0
		for key := range m.GetVal() {
			sorted_keys[i] = key
			i++
		}
		sort.Slice(sorted_keys, func(i, j int) bool { return sorted_keys[i] < sorted_keys[j] })
		for _, key := range sorted_keys {
			val := m.GetVal()[key]
			_ = val

			if _, ok := _MapKeysValuesIn_Val_InLookup[key]; !ok {
				err := MapKeysValuesInValidationError{
					field:  fmt.Sprintf("Val[%v]", key),
					reason: "value must be in list [foo bar]",
				}
				if !all {
					return err
				}
				errors = append(errors, err)
			}

			if _, ok := _MapKeysValuesIn_Val_InLookup[val]; !ok {
				err := MapKeysValuesInValidationError{
					field:  fmt.Sprintf("Val[%v]", key),
					reason: "value must be in list [baz qux]",
				}
				if !all {
					return err
				}
				errors = append(errors, err)
			}

		}
	}

	if len(errors) > 0 {
		return MapKeysValuesInMultiError(errors)
	}

	return nil
}

There are two issues that need to be solved

  1. The lookup (_MapKeysValuesIn_Val_InLookup) is not defined anywhere, so the code doesn't compile.
  2. The keys and values validations both refer to the same _MapKeysValuesIn_Val_InLookup, but they should be referring to different lookups.
@haines
Copy link
Author

haines commented Nov 29, 2022

#619
#673

@haines haines closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2022
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

No branches or pull requests

1 participant