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

Allow As*Map* and As*Array* functions to perform inner conversions #11348

Closed
iwahbe opened this issue Nov 14, 2022 · 1 comment · Fixed by #11351
Closed

Allow As*Map* and As*Array* functions to perform inner conversions #11348

iwahbe opened this issue Nov 14, 2022 · 1 comment · Fixed by #11351
Assignees
Labels
area/sdks Pulumi language SDKs kind/enhancement Improvements or new features language/go resolution/fixed This issue was fixed
Milestone

Comments

@iwahbe
Copy link
Member

iwahbe commented Nov 14, 2022

Hello!

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

Calling any of our As*Map* or As*Array* methods performs a direct cast on the awaited value, with no attempt to ensure the conversion is valid. This is particularly painful in go, since it is not valid to case a value x from []interface{} to []string, even if every value in x is itself a string. We should modify this behavior to be more tolerant, allowing casts where each inner type is a valid cast.

This change can be most easily expressed by showing the new code:

old code:

func (a AnyOutput) AsStringMapOutput() StringMapOutput {
	return a.ApplyT(func(i interface{}) map[string]string {
		return i.(map[string]string)
	}).(StringMapOutput)
}

new code:

func (a AnyOutput) AsStringMapOutput() StringMapOutput {
	return a.ApplyT(func(i interface{}) map[string]string {
		v, ok := i.(map[string]string)
                if ok { return v }
		v := map[string]string{}
		vAny, ok := i.(map[string]interface{})
                if !ok { 
			panic(fmt.Sprintf("expected map[string]string or map[string]interface{}, found %T", i))
		}
		for k, val := range vAny {
			sVal, ok := val.(string)
			if !ok {
				panic(fmt.Sprintf("Key %s has an invalid value of type %T, must be a string", val)
			}
			v[k] = sVal
		}
		return v
	}).(StringMapOutput)
}

Inspired by #11347.

Affected area/feature

@iwahbe iwahbe added area/sdks Pulumi language SDKs kind/enhancement Improvements or new features language/go needs-triage Needs attention from the triage team labels Nov 14, 2022
@iwahbe
Copy link
Member Author

iwahbe commented Nov 14, 2022

Partially implemented by #10991.

@iwahbe iwahbe self-assigned this Nov 14, 2022
@iwahbe iwahbe removed the needs-triage Needs attention from the triage team label Nov 14, 2022
@bors bors bot closed this as completed in 16afb72 Nov 14, 2022
@pulumi-bot pulumi-bot added the resolution/fixed This issue was fixed label Nov 14, 2022
@iwahbe iwahbe added this to the 0.81 milestone Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/sdks Pulumi language SDKs kind/enhancement Improvements or new features language/go resolution/fixed This issue was fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants