expr icon indicating copy to clipboard operation
expr copied to clipboard

Map Access does not have check

Open seekerkoruth opened this issue 4 years ago • 4 comments

I am trying to put a check in map access.

if you have a Golang map foo and it does not have an element "bar" the code below will fail silently without an error message regarding non existent map element. It returns a 0 and the silent nature of this substitution and error can create issues.

foo.bar+1

I suggest in line 37 of runtime.go:

	case reflect.Map:
		value := v.MapIndex(reflect.ValueOf(i))
		if value.IsValid() {
			if value.CanInterface() {
				return value.Interface()
			}
		} else {
			panic(fmt.Sprintf("%v not found", i))        // this is the addition
			//elem := reflect.TypeOf(from).Elem()       // this is commented
			//return reflect.Zero(elem).Interface()       // this is commented
		}

Thank you.

seekerkoruth avatar Jul 04 '21 12:07 seekerkoruth

I don’t remember the logic behind this behaviour. Maybe @davidklassen has some thoughts?

antonmedv avatar Jul 04 '21 17:07 antonmedv

I don’t remember the logic behind this behaviour. Maybe @davidklassen has some thoughts?

@antonmedv ,

The above code fragment will fix it. runtime.go:37

seekerkoruth avatar Jul 04 '21 20:07 seekerkoruth

Not just a fix. Expr will behave differently.

antonmedv avatar Jul 04 '21 20:07 antonmedv

Not just a fix. Expr will behave differently.

@antonmedv,

Understood. However it will be in the line of other checks made such as a slice index out of bounds error. Etc.

bar not found (1:5)
 | foo.bar+1
 | ....^

Regardless .. excellent work with the rest.

Respectfully.

seekerkoruth avatar Jul 04 '21 21:07 seekerkoruth

Added a such check, fixed in master.

antonmedv avatar Nov 05 '22 15:11 antonmedv