github.com/opentofu/opentofu@v1.7.1/internal/encryption/compliancetest/config_struct.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package compliancetest
     7  
     8  import (
     9  	"reflect"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func ConfigStruct[TConfig any](t *testing.T, configStruct any) {
    15  	Log(t, "Testing config struct compliance...")
    16  	if configStruct == nil {
    17  		Fail(t, "The ConfigStruct() method on the descriptor returns a nil configuration. Please implement this function correctly.")
    18  	} else {
    19  		Log(t, "The ConfigStruct() method returned a non-nil value.")
    20  	}
    21  
    22  	configStructPtrType := reflect.TypeOf(configStruct)
    23  	if configStructPtrType.Kind() != reflect.Ptr {
    24  		Fail(t, "The ConfigStruct() method returns a %T, but it should return a pointer to a struct.", configStruct)
    25  	} else {
    26  		Log(t, "The ConfigStruct() method returned a pointer.")
    27  	}
    28  	configStructType := configStructPtrType.Elem()
    29  	if configStructType.Kind() != reflect.Struct {
    30  		Fail(t, "The ConfigStruct() method returns a pointer to %s, but it should return a pointer to a struct.", configStructType.Elem().Name())
    31  	} else {
    32  		Log(t, "The ConfigStruct() method returned a pointer to a struct.")
    33  	}
    34  
    35  	typedConfigStruct, ok := configStruct.(TConfig)
    36  	if !ok {
    37  		Fail(t, "The ConfigStruct() method returns a %T instead of a %T", configStruct, typedConfigStruct)
    38  	} else {
    39  		Log(t, "The ConfigStruct() method correctly returns a %T", typedConfigStruct)
    40  	}
    41  
    42  	hclTagFound := false
    43  	for i := 0; i < configStructType.NumField(); i++ {
    44  		field := configStructType.Field(i)
    45  		hclTag, ok := field.Tag.Lookup("hcl")
    46  		if !ok {
    47  			continue
    48  		}
    49  		hclTagFound = true
    50  		if hclTag == "" {
    51  			Fail(
    52  				t,
    53  				"The field '%s' on the config structure %s has an empty HCL tag. Please remove the hcl tag or add a value that matches %s.",
    54  				field.Name,
    55  				configStructType.Name(),
    56  				hclTagRe,
    57  			)
    58  		} else {
    59  			Log(t, "Found a non-empty hcl tag on field '%s' of %s.", field.Name, configStructType.Name())
    60  		}
    61  		hclTagParts := strings.Split(hclTag, ",")
    62  		if !hclTagRe.MatchString(hclTagParts[0]) {
    63  			Fail(
    64  				t,
    65  				"The field '%s' on the config structure %s has an invalid hcl tag: %s. Please add a value that matches %s.",
    66  				field.Name,
    67  				configStructType.Name(),
    68  				hclTag,
    69  				hclTagRe,
    70  			)
    71  		} else {
    72  			Log(t, "Found hcl tag on field '%s' of %s matches the name requirements.", field.Name, configStructType.Name())
    73  		}
    74  	}
    75  	if !hclTagFound {
    76  		Fail(
    77  			t,
    78  			"The configuration struct %s does not contain any fields with hcl tags, which means users will not be able to configure this key provider. Please provide at least one field with an hcl tag.",
    79  			configStructType.Name(),
    80  		)
    81  	} else {
    82  		Log(t, "Found at least one field with a hcl tag.")
    83  	}
    84  
    85  }