Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/20250404155011.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkle: Add IMethod generation for Items that are not in Collections
32 changes: 32 additions & 0 deletions generator/codegen/collection_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type NotificationFeedCollections []NotificationFeedCollection

type CollectionParams = struct {
Collections
JobItems
MessageCollections
NotificationFeedCollections
}
Expand Down Expand Up @@ -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]()
Expand Down Expand Up @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions generator/codegen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
17 changes: 5 additions & 12 deletions generator/codegen/jobitem_extensions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package codegen

import (
"encoding/json"
"slices"
"strings"

Expand All @@ -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")
}
Expand All @@ -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})
}
}
Expand Down
29 changes: 29 additions & 0 deletions generator/codegen/templates/entities.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading