From 1a26252b61d30c4ad877ade4b7181c710c1c5651 Mon Sep 17 00:00:00 2001 From: Tristan Cartledge Date: Sat, 4 Nov 2023 11:41:52 +0000 Subject: [PATCH] feat: add config for maintaining openapi order and fix bug with comments defaults --- config.go | 33 ++++++++++++++++++++++++++++++--- io_test.go | 9 +++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 922e8d8..d3c2475 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,8 @@ package config import ( + "strings" + "github.com/AlekSi/pointer" "github.com/mitchellh/mapstructure" ) @@ -40,6 +42,7 @@ type Generation struct { SingleTagPerOp bool `yaml:"singleTagPerOp,omitempty"` TagNamespacingDisabled bool `yaml:"tagNamespacingDisabled,omitempty"` RepoURL string `yaml:"repoURL,omitempty"` + MaintainOpenAPIOrder bool `yaml:"maintainOpenAPIOrder,omitempty"` } type DevContainers struct { @@ -149,7 +152,25 @@ func GetDefaultConfig(newSDK bool, getLangDefaultFunc GetLanguageDefaultFunc, la fields := map[string]any{} for _, field := range defaults { if field.DefaultValue != nil { - fields[field.Name] = *field.DefaultValue + if strings.Contains(field.Name, ".") { + parts := strings.Split(field.Name, ".") + + currMap := fields + + for i, part := range parts { + if i == len(parts)-1 { + currMap[part] = *field.DefaultValue + } else { + if _, ok := currMap[part]; !ok { + currMap[part] = map[string]any{} + } + + currMap = currMap[part].(map[string]any) + } + } + } else { + fields[field.Name] = *field.DefaultValue + } } } @@ -225,17 +246,23 @@ func GetGenerationDefaults(newSDK bool) []SDKGenConfigField { Description: pointer.To("Operations with multiple tags will only generate methods namespaced by the first tag"), }, { - Name: "disableComments", + Name: "comments.disableComments", Required: false, DefaultValue: ptr(false), Description: pointer.To("Disable generating comments from spec on the SDK"), }, { - Name: "omitDescriptionIfSummaryPresent", + Name: "comments.omitDescriptionIfSummaryPresent", Required: false, DefaultValue: ptr(false), Description: pointer.To("Omit generating comment descriptions if spec provides a summary"), }, + { + Name: "maintainOpenAPIOrder", + Required: false, + DefaultValue: ptr(newSDK), + Description: pointer.To("Maintains the order of things like parameters and fields when generating the SDK"), + }, } } diff --git a/io_test.go b/io_test.go index 3f4545c..7ef7ac6 100644 --- a/io_test.go +++ b/io_test.go @@ -36,8 +36,13 @@ func TestLoad_Success(t *testing.T) { }, }, Generation: Generation{ - SDKClassName: "SDK", - SingleTagPerOp: false, + Comments: &Comments{ + DisableComments: false, + OmitDescriptionIfSummaryPresent: false, + }, + SDKClassName: "SDK", + SingleTagPerOp: false, + MaintainOpenAPIOrder: true, }, New: map[string]bool{ "go": true,