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

Code before:
	}

	return cty.ObjectVal(mockValues), diags
}

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.",
			))
		}

		/*
			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
		*/

		overrideValue, hasOverride := overrides[k]
		configValue, hasConfig := configMap[k]
		// If the configured value is null, it is the same as not specified
		hasConfig = hasConfig && !configValue.IsNull()

		var ovConvert cty.Value


Review comment (suggestion):
It's really helpful to have this truth table! I think this should be moved to the function declaration, so it's easy to see when hovering the `composeMockValueForAttributes` from other places.

Code after:
	}

	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.",
			))
		}

		overrideValue, hasOverride := overrides[k]
		configValue, hasConfig := configMap[k]
		// If the configured value is null, it is the same as not specified
		hasConfig = hasConfig && !configValue.IsNull()

		var ovConvert cty.Value