Repository: opentofu/opentofu
File: internal/configs/hcl2shim/mock_value_composer.go (Go)

Code before:
	}

	for k := range overrides {
		if _, ok := impliedTypes[k]; !ok {
			diags = diags.Append(tfdiags.WholeContainingBody(
				tfdiags.Error,
				fmt.Sprintf("Invalid override for block field `%v`", k),
				"The field is unknown. Please, ensure it is a part of resource definition.",
			))
		}
	}

	return cty.ObjectVal(mockValues), diags
}

/*
composeMockValueForAttributes follows the truth table below for generating an output value, given schema and inputs:
Required  Optional  Computed  Has config  Has Override  Result
t         f         f         t           t             Error - Not Allowed to override config
t         f         f         t           f             Config
t         f         f         f           t             Error - Required field in config not provided
t         f         f         f           f             Error - Required field in config not provided
f         t         f         t           t             Error - Not Allowed to override config
f         t         f         t           f             Config
f         t         f         f           t             Override Value
f         t         f         f           f             NilVal of the attribute type
f         t         t         t           t             Error - Not Allowed to override config
f         t         t         t           f             Config
f         t         t         f           t             Override
f         t         t         f           f             GenVal
f         f         t         t           t             Error - Not Allowed to override config
f         f         t         t           f             Error - Config not allowed here
f         f         t         f           t             Override
f         f         t         f           f             GenVal
*/
func (mvc MockValueComposer) composeMockValueForAttributes(attrs map[string]*configschema.Attribute, configMap map[string]cty.Value, overrides map[string]cty.Value) (map[string]cty.Value, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	mockAttrs := make(map[string]cty.Value)

	// Stable order is important here so random values match its fields between function calls.
	for _, kv := range mapToSortedSlice(attrs) {
		k, attr := kv.k, kv.v

		if attr.NestedType != nil && attr.NestedType.Nesting == configschema.NestingGroup {
			// This should not be possible to hit.  Neither tofu or the provider framework will allow
			// NestingGroup in here.  However, this could change at some point and we want to be prepared for it.
			diags = diags.Append(tfdiags.WholeContainingBody(
				tfdiags.Error,
				fmt.Sprintf("Unsupported field `%v` in attribute mocking", k),
				"Overriding non-computed fields is not allowed, so this field cannot be processed.",

Review comment (suggestion):
`cty.NilVal` (the zero value of `cty.Value` from the perspective of Go code) should never appear as part of another value, though it seems like the code below uses `cty.NullVal` (as I would've expected) so I think this is just a problem with this comment, and not with the behavior.


Code after:
	}

	for k := range overrides {
		if _, ok := impliedTypes[k]; !ok {
			diags = diags.Append(tfdiags.WholeContainingBody(
				tfdiags.Error,
				fmt.Sprintf("Invalid override for block field `%v`", k),
				"The field is unknown. Please, ensure it is a part of resource definition.",
			))
		}
	}

	return cty.ObjectVal(mockValues), diags
}

/*
composeMockValueForAttributes follows the truth table below for generating an output value, given schema and inputs:
Required  Optional  Computed  Has config  Has Override  Result
t         f         f         t           t             Error - Not Allowed to override config
t         f         f         t           f             Config
t         f         f         f           t             Error - Required field in config not provided
t         f         f         f           f             Error - Required field in config not provided
f         t         f         t           t             Error - Not Allowed to override config
f         t         f         t           f             Config
f         t         f         f           t             Override Value
f         t         f         f           f             NullVal of the attribute type
f         t         t         t           t             Error - Not Allowed to override config
f         t         t         t           f             Config
f         t         t         f           t             Override
f         t         t         f           f             GenVal
f         f         t         t           t             Error - Not Allowed to override config
f         f         t         t           f             Error - Config not allowed here
f         f         t         f           t             Override
f         f         t         f           f             GenVal
*/
func (mvc MockValueComposer) composeMockValueForAttributes(attrs map[string]*configschema.Attribute, configMap map[string]cty.Value, overrides map[string]cty.Value) (map[string]cty.Value, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	mockAttrs := make(map[string]cty.Value)

	// Stable order is important here so random values match its fields between function calls.
	for _, kv := range mapToSortedSlice(attrs) {
		k, attr := kv.k, kv.v

		if attr.NestedType != nil && attr.NestedType.Nesting == configschema.NestingGroup {
			// This should not be possible to hit.  Neither tofu or the provider framework will allow
			// NestingGroup in here.  However, this could change at some point and we want to be prepared for it.
			diags = diags.Append(tfdiags.WholeContainingBody(
				tfdiags.Error,
				fmt.Sprintf("Unsupported field `%v` in attribute mocking", k),
				"Overriding non-computed fields is not allowed, so this field cannot be processed.",