From 154ff7abb054ac057c787cffcb1f0ef688ebc0cd Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 13 Oct 2023 16:07:55 +0200 Subject: [PATCH] devcenter: updating `Max` to `Maximum` and updating the website category Please enter the commit message for your changes. Lines starting --- config/resources/devcenter.hcl | 2 +- .../processors/field_name_max_to_maximum.go | 34 +++++++++++ .../field_name_max_to_maximum_test.go | 56 +++++++++++++++++++ .../components/schema/processors/fields.go | 1 + 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum.go create mode 100644 tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum_test.go diff --git a/config/resources/devcenter.hcl b/config/resources/devcenter.hcl index f45c899254b..bc5d1eceb1b 100644 --- a/config/resources/devcenter.hcl +++ b/config/resources/devcenter.hcl @@ -18,7 +18,7 @@ service "DevCenter" { definition "dev_center_project" { id = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevCenter/projects/{projectName}" display_name = "Dev Center Project" - website_subcategory = "Dev Center Project" + website_subcategory = "Dev Center" description = "Manages a Dev Center Project" } } diff --git a/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum.go b/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum.go new file mode 100644 index 00000000000..dfa43537365 --- /dev/null +++ b/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum.go @@ -0,0 +1,34 @@ +package processors + +import ( + "fmt" + "github.com/hashicorp/go-azure-helpers/lang/pointer" + "strings" + "unicode" +) + +var _ FieldNameProcessor = fieldNameMaxToMaximum{} + +type fieldNameMaxToMaximum struct{} + +func (fieldNameMaxToMaximum) ProcessField(fieldName string, metadata FieldMetadata) (*string, error) { + if strings.HasPrefix(strings.ToLower(fieldName), "max") { + // if this is the literal value `Max` + if strings.EqualFold(fieldName, "max") { + return pointer.To("Maximum"), nil + } + + // at this point the word must be longer than 3 words, so we can safely trim it + trimmedValue := fieldName[3:] + firstChar := trimmedValue[0] + if !unicode.IsUpper(rune(firstChar)) { + return nil, nil + } + + // If this was `Max{Something}` (being another word) then we should swap this out: + trimmedValue = fmt.Sprintf("Maximum%s", trimmedValue) + return pointer.To(trimmedValue), nil + } + + return nil, nil +} diff --git a/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum_test.go b/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum_test.go new file mode 100644 index 00000000000..ddcdffc7442 --- /dev/null +++ b/tools/importer-rest-api-specs/components/schema/processors/field_name_max_to_maximum_test.go @@ -0,0 +1,56 @@ +package processors + +import ( + "testing" +) + +func TestFieldNameMaxToMaximum_Valid(t *testing.T) { + result, err := fieldNameMaxToMaximum{}.ProcessField("MaxValue", FieldMetadata{}) + if err != nil { + t.Fatalf(err.Error()) + } + + if result == nil { + t.Fatalf("expected result to be non-nil but it was") + } + if *result != "MaximumValue" { + t.Fatalf("expected result to be `MaximumValue` but got %q", *result) + } +} + +func TestFieldNameMaxToMaximum_Invalid(t *testing.T) { + result, err := fieldNameMaxToMaximum{}.ProcessField("Maximillian", FieldMetadata{}) + if err != nil { + t.Fatalf(err.Error()) + } + + if result != nil { + t.Fatalf("expected result to be nil but got %q", *result) + } +} + +func TestFieldNameMaxToMaximum_LiteralValueOfMax(t *testing.T) { + result, err := fieldNameMaxToMaximum{}.ProcessField("Max", FieldMetadata{}) + if err != nil { + t.Fatalf(err.Error()) + } + if result == nil { + t.Fatalf("expected result to be non-nil but it was") + } + if *result != "Maximum" { + t.Fatalf("expected result to be `Maximum` but got %q", *result) + } +} + +func TestFieldNameMaxToMaximum_LiteralValueOfMaxLowerCase(t *testing.T) { + result, err := fieldNameMaxToMaximum{}.ProcessField("max", FieldMetadata{}) + if err != nil { + t.Fatalf(err.Error()) + } + if result == nil { + t.Fatalf("expected result to be non-nil but it was") + } + if *result != "Maximum" { + t.Fatalf("expected result to be `Maximum` but got %q", *result) + } +} diff --git a/tools/importer-rest-api-specs/components/schema/processors/fields.go b/tools/importer-rest-api-specs/components/schema/processors/fields.go index 69663a346b5..448cc51b304 100644 --- a/tools/importer-rest-api-specs/components/schema/processors/fields.go +++ b/tools/importer-rest-api-specs/components/schema/processors/fields.go @@ -22,6 +22,7 @@ var NamingRules = []FieldNameProcessor{ fieldNameRemoveResourcePrefix{}, fieldNameRenameBoolean{}, fieldNameRenameMislabelledResourceID{}, + fieldNameMaxToMaximum{}, } //TODO: if it's a List[Reference] and the model contains a single field `Id` then flatten this into `_ids`.