diff --git a/changes/20250404155011.feature b/changes/20250404155011.feature new file mode 100644 index 00000000..9fa0f4a1 --- /dev/null +++ b/changes/20250404155011.feature @@ -0,0 +1 @@ +:sparkle: Add IMethod generation for Items that are not in Collections diff --git a/generator/codegen/collection_extensions.go b/generator/codegen/collection_extensions.go index 01364b6d..2e73fbf1 100644 --- a/generator/codegen/collection_extensions.go +++ b/generator/codegen/collection_extensions.go @@ -36,6 +36,7 @@ type NotificationFeedCollections []NotificationFeedCollection type CollectionParams = struct { Collections + JobItems MessageCollections NotificationFeedCollections } @@ -236,6 +237,30 @@ func newNotificationFeedCollection(itemRef string) NotificationFeedCollection { } } +func getStandaloneJobItems(swagger *openapi3.T, collections []Collection) (standalone JobItems, err error) { + jobsParams, subErr := GetJobItems(swagger) + if subErr != nil { + err = subErr + return + } + + jobSet := mapset.NewSet[JobItem]() + for _, jobItem := range jobsParams.JobItems { + jobSet.Add(jobItem) + } + + for _, collection := range collections { + jobTarget := JobItem{collection.ItemRef} + if jobSet.Contains(jobTarget) { + jobSet.Remove(jobTarget) + } + } + + standalone = jobSet.ToSlice() + + return +} + func GetCollections(swagger *openapi3.T) (collections CollectionParams, err error) { collectionSet := mapset.NewSet[Collection]() messageCollectionsSet := mapset.NewSet[MessageCollection]() @@ -324,8 +349,15 @@ func GetCollections(swagger *openapi3.T) (collections CollectionParams, err erro slices.SortFunc(messageCollectionSlice, func(a, b MessageCollection) int { return strings.Compare(a.ItemRef, b.ItemRef) }) slices.SortFunc(notificationFeedCollectionSlice, func(a, b NotificationFeedCollection) int { return strings.Compare(a.ItemRef, b.ItemRef) }) + standaloneJobItems, subErr := getStandaloneJobItems(swagger, collectionSlice) + if subErr != nil { + err = subErr + return + } + collections = CollectionParams{ collectionSlice, + standaloneJobItems, messageCollectionSlice, notificationFeedCollectionSlice, } diff --git a/generator/codegen/common.go b/generator/codegen/common.go index 3096a479..3bc1cbf3 100644 --- a/generator/codegen/common.go +++ b/generator/codegen/common.go @@ -26,9 +26,11 @@ var ( ) const ( - schemaPrefix = "#/components/schemas/" - jsonMIME = "application/json" + schemaPrefix = "#/components/schemas/" + jsonMIME = "application/json" + // See https://github.com/Arm-Debug/API-Uniform-Contract?tab=readme-ov-file#api-extensions redactFlag = "x-redact" + jobFlag = "x-job" collectionFlag = "x-collection" ) diff --git a/generator/codegen/jobitem_extensions.go b/generator/codegen/jobitem_extensions.go index 98a1ac53..e197ad62 100644 --- a/generator/codegen/jobitem_extensions.go +++ b/generator/codegen/jobitem_extensions.go @@ -1,7 +1,6 @@ package codegen import ( - "encoding/json" "slices" "strings" @@ -19,10 +18,6 @@ type JobItemsParams = struct { JobItems } -const ( - JobItemName = "x-job" // See https://github.com/Arm-Debug/API-Uniform-Contract?tab=readme-ov-file#api-extensions -) - func AddJobItemsToParams(d *Data) (err error) { return AddValuesToParams(d, func(swagger *openapi3.T) (interface{}, error) { return GetJobItems(swagger) }, "jobs.go") } @@ -45,14 +40,12 @@ func GetJobItems(swagger *openapi3.T) (jobItems JobItemsParams, err error) { continue } - var isExtendedJobItem bool - if c, ok := schemaVal.ExtensionProps.Extensions[JobItemName].(json.RawMessage); ok { - err = json.Unmarshal(c, &isExtendedJobItem) - if err != nil { - return - } + isJobItem, subErr := isExtensionFlagSet(schemaVal.ExtensionProps, jobFlag) + if subErr != nil { + err = subErr + return } - if isExtendedJobItem { + if isJobItem { jobs = append(jobs, JobItem{schemaName}) } } diff --git a/generator/codegen/templates/entities.go.tmpl b/generator/codegen/templates/entities.go.tmpl index be5a4ad1..be87d79a 100644 --- a/generator/codegen/templates/entities.go.tmpl +++ b/generator/codegen/templates/entities.go.tmpl @@ -140,6 +140,35 @@ func New{{ .CollectionRef }}Collection() IStaticPage { return New{{ .CollectionRef }}WithDefaults() } {{ end }} +{{ range .JobItems -}} +// ============================================================================================ +// This extends {{ .JobItemSchema }} definitions +// ============================================================================================ +// FetchType returns the resource type +func (o *{{ .JobItemSchema }}) FetchType() string { + return "{{ .JobItemSchema }}" +} + +// FetchLinks returns the resource links if present +func (o *{{ .JobItemSchema }}) FetchLinks() (links any, err error) { + if !o.Links.IsSet() { + err = errors.New("missing links") + return + } + links = o.GetLinks() + return +} + +// FetchName returns the resource name if present, or else an error +func (o *{{ .JobItemSchema }}) FetchName() (string, error) { + return o.GetName(), nil +} + +// FetchTitle returns the resource title if present, or else an error +func (o *{{ .JobItemSchema }}) FetchTitle() (string, error) { + return o.GetTitle(), nil +} +{{ end }} {{ range .MessageCollections -}} // ============================================================================================ // This extends {{ .ItemRef }} definitions