Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate backreference links #17

Merged
merged 1 commit into from
Dec 13, 2017
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
42 changes: 0 additions & 42 deletions api/builtin/api_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -21,54 +20,13 @@ 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)
}

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 != "" {
Expand Down
26 changes: 18 additions & 8 deletions api/writer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,24 @@ func (j *JSONResponseWriter) convert(b *builder.Builder, context *types.APIConte
}

func (j *JSONResponseWriter) addLinks(b *builder.Builder, schema *types.Schema, context *types.APIContext, input map[string]interface{}, rawResource *types.RawResource) {
if rawResource.ID != "" {
self := context.URLBuilder.ResourceLink(rawResource)
rawResource.Links["self"] = self
if schema.CanUpdate() {
rawResource.Links["update"] = self
}
if schema.CanDelete() {
rawResource.Links["remove"] = self
if rawResource.ID == "" {
return
}

self := context.URLBuilder.ResourceLink(rawResource)
rawResource.Links["self"] = self
if schema.CanUpdate() {
rawResource.Links["update"] = self
}
if schema.CanDelete() {
rawResource.Links["remove"] = self
}

for _, backRef := range context.Schemas.References(schema) {
if schema.SubContext == "" {
rawResource.Links[backRef.Schema.PluralName] = context.URLBuilder.FilterLink(backRef.Schema, backRef.FieldName, rawResource.ID)
} else {
rawResource.Links[backRef.Schema.PluralName] = context.URLBuilder.SubContextCollection(schema, rawResource.ID, backRef.Schema)
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func DefaultURLParser(schemas *types.Schemas, url *url.URL) (ParsedURL, error) {
path = multiSlashRegexp.ReplaceAllString(path, "/")

parts := strings.SplitN(path[len(version.Path):], "/", 4)
prefix, parts, subContext := parseSubContext(version, parts)
prefix, parts, subContext := parseSubContext(schemas, version, parts)

result.Version = version.Path
result.SubContext = subContext
Expand Down Expand Up @@ -139,7 +139,7 @@ func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, ur
return result, nil
}

func parseSubContext(version *types.APIVersion, parts []string) (string, []string, map[string]string) {
func parseSubContext(schemas *types.Schemas, version *types.APIVersion, parts []string) (string, []string, map[string]string) {
subContext := ""
result := map[string]string{}

Expand All @@ -151,6 +151,11 @@ func parseSubContext(version *types.APIVersion, parts []string) (string, []strin
break
}

subSchema := schemas.Schema(version, parts[3])
if subSchema == nil {
break
}

result[resourceType] = resourceID
subContext = subContext + "/" + resourceType + "/" + resourceID
parts = append(parts[:1], parts[3:]...)
Expand Down
29 changes: 29 additions & 0 deletions types/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/rancher/norman/name"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/definition"
)

type SchemaCollection struct {
Expand All @@ -17,10 +18,16 @@ type SchemaInitFunc func(*Schemas) *Schemas

type MappersFactory func() []Mapper

type BackReference struct {
FieldName string
Schema *Schema
}

type Schemas struct {
schemasByPath map[string]map[string]*Schema
schemasBySubContext map[string]*Schema
mappers map[string]map[string][]Mapper
references map[string][]BackReference
DefaultMappers MappersFactory
DefaultPostMappers MappersFactory
versions []APIVersion
Expand All @@ -33,6 +40,7 @@ func NewSchemas() *Schemas {
schemasByPath: map[string]map[string]*Schema{},
schemasBySubContext: map[string]*Schema{},
mappers: map[string]map[string][]Mapper{},
references: map[string][]BackReference{},
}
}

Expand Down Expand Up @@ -92,6 +100,22 @@ func (s *Schemas) AddSchema(schema Schema) *Schemas {
if _, ok := schemas[schema.ID]; !ok {
schemas[schema.ID] = &schema
s.schemas = append(s.schemas, &schema)

for name, field := range schema.ResourceFields {
if !definition.IsReferenceType(field.Type) {
continue
}

refType := definition.SubType(field.Type)
if !strings.HasPrefix(refType, "/") {
refType = convert.ToFullReference(schema.Version.Path, refType)
}

s.references[refType] = append(s.references[refType], BackReference{
FieldName: name,
Schema: &schema,
})
}
}

if schema.SubContext != "" {
Expand All @@ -101,6 +125,11 @@ func (s *Schemas) AddSchema(schema Schema) *Schemas {
return s
}

func (s *Schemas) References(schema *Schema) []BackReference {
refType := convert.ToFullReference(schema.Version.Path, schema.ID)
return s.references[refType]
}

func (s *Schemas) AddMapper(version *APIVersion, schemaID string, mapper Mapper) *Schemas {
mappers, ok := s.mappers[version.Path]
if !ok {
Expand Down
1 change: 1 addition & 0 deletions types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type URLBuilder interface {
ReverseSort(order SortOrder) string
Sort(field string) string
SetSubContext(subContext string)
FilterLink(schema *Schema, fieldName string, value string) string
}

type Store interface {
Expand Down
5 changes: 5 additions & 0 deletions urlbuilder/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (u *urlBuilder) Version(version types.APIVersion) string {
return u.constructBasicURL(version)
}

func (u *urlBuilder) FilterLink(schema *types.Schema, fieldName string, value string) string {
return u.constructBasicURL(schema.Version, schema.PluralName) + "?" +
url.QueryEscape(fieldName) + "=" + url.QueryEscape(value)
}

func (u *urlBuilder) constructBasicURL(version types.APIVersion, parts ...string) string {
buffer := bytes.Buffer{}

Expand Down