From fc6948e3c67c01401b974677f01fbec1624c6379 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Wed, 29 May 2024 22:32:35 +0300 Subject: [PATCH] Add config.Resource.RemoveSingletonListConversion to be able to remove already configured singleton list conversions. - The main use case is to prevent singleton list conversions for configuration arguments with single nested blocks. Signed-off-by: Alper Rifat Ulucinar --- pkg/config/resource.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/config/resource.go b/pkg/config/resource.go index e7002629..de014834 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -693,6 +693,28 @@ func (r *Resource) AddSingletonListConversion(tfPath, crdPath string) { r.listConversionPaths[tfPath] = crdPath } +// RemoveSingletonListConversion removes the singleton list conversion path +// for the specified Terraform configuration path. The specified fieldpath +// expression must be a Terraform field path with or without the wildcard +// segments. Returns true if the path has already been registered for +// singleton list conversion. +func (r *Resource) RemoveSingletonListConversion(tfPath string) bool { + nPath := strings.ReplaceAll(tfPath, "[*]", "") + nPath = strings.ReplaceAll(nPath, "[0]", "") + for p := range r.listConversionPaths { + n := strings.ReplaceAll(p, "[*]", "") + n = strings.ReplaceAll(n, "[0]", "") + if n == nPath { + delete(r.listConversionPaths, p) + if r.SchemaElementOptions[n] != nil { + r.SchemaElementOptions[n].EmbeddedObject = false + } + return true + } + } + return false +} + // SetEmbeddedObject sets the EmbeddedObject for the specified key. // The key is a Terraform field path without the wildcard segments. func (m SchemaElementOptions) SetEmbeddedObject(el string) {