1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-01 23:36:58 +00:00

Generate backreference links

This commit is contained in:
Darren Shepherd
2017-12-13 08:53:28 -07:00
parent d0aef1eafd
commit 9dcef33943
6 changed files with 60 additions and 52 deletions

View File

@@ -3,7 +3,6 @@ package builtin
import (
"github.com/rancher/norman/store/empty"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)
func APIRootFormatter(apiContext *types.APIContext, resource *types.RawResource) {
@@ -21,22 +20,6 @@ func APIRootFormatter(apiContext *types.APIContext, resource *types.RawResource)
resource.Links["self"] = apiContext.URLBuilder.Version(apiVersion)
if len(apiVersion.SubContexts) > 0 {
subContextToSchema := apiContext.Schemas.SubContextSchemas()
if len(subContextToSchema) > 0 {
for _, schema := range subContextToSchema {
addCollectionLink(apiContext, schema, resource.Links)
}
for _, schema := range getNonReferencedSchemas(apiContext.Schemas.SchemasForVersion(apiVersion),
subContextToSchema) {
addCollectionLink(apiContext, schema, resource.Links)
}
return
}
}
for _, schema := range apiContext.Schemas.SchemasForVersion(apiVersion) {
addCollectionLink(apiContext, schema, resource.Links)
}
@@ -44,31 +27,6 @@ func APIRootFormatter(apiContext *types.APIContext, resource *types.RawResource)
return
}
func getNonReferencedSchemas(schemas map[string]*types.Schema, subContexts map[string]*types.Schema) []*types.Schema {
var result []*types.Schema
typeNames := map[string]bool{}
for _, subContext := range subContexts {
ref := convert.ToReference(subContext.ID)
fullRef := convert.ToFullReference(subContext.Version.Path, subContext.ID)
typeNames[ref] = true
typeNames[fullRef] = true
}
outer:
for _, schema := range schemas {
for _, field := range schema.ResourceFields {
if typeNames[field.Type] {
continue outer
}
}
result = append(result, schema)
}
return result
}
func addCollectionLink(apiContext *types.APIContext, schema *types.Schema, links map[string]string) {
collectionLink := getSchemaCollectionLink(apiContext, schema, nil)
if collectionLink != "" {