-
Notifications
You must be signed in to change notification settings - Fork 135
/
base_entity_api_model.go
122 lines (98 loc) · 3.06 KB
/
base_entity_api_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api_impl
import (
"github.com/go-openapi/strfmt"
"github.com/openziti/ziti/controller/models"
"github.com/openziti/ziti/controller/rest_model"
"path"
)
func BaseEntityToRestModel(entity models.Entity, linkFactory LinksFactory) rest_model.BaseEntity {
id := entity.GetId()
createdAt := strfmt.DateTime(entity.GetCreatedAt())
updatedAt := strfmt.DateTime(entity.GetUpdatedAt())
tags := rest_model.Tags{
SubTags: entity.GetTags(),
}
ret := rest_model.BaseEntity{
ID: &id,
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
Links: linkFactory.Links(entity),
Tags: &tags,
}
if ret.Tags.SubTags == nil {
ret.Tags.SubTags = map[string]interface{}{}
}
return ret
}
type FullLinkFactory interface {
LinksFactory
SelfLinkFactory
}
type LinksFactory interface {
Links(entity LinkEntity) rest_model.Links
EntityName() string
}
type SelfLinkFactory interface {
SelfLink(entity models.Entity) rest_model.Link
}
type CreateLinkFactory interface {
SelfLinkFromId(id string) rest_model.Link
}
func NewBasicLinkFactory(entityName string) *BasicLinkFactory {
return &BasicLinkFactory{entityName: entityName}
}
type BasicLinkFactory struct {
entityName string
}
func (factory *BasicLinkFactory) SelfLinkFromId(id string) rest_model.Link {
return NewLink(factory.SelfUrlString(id))
}
func (factory *BasicLinkFactory) SelfUrlString(id string) string {
//path.Join will remove the ./ prefix in its "clean" operation
return "./" + path.Join(factory.entityName, id)
}
func (factory *BasicLinkFactory) SelfLink(entity LinkEntity) rest_model.Link {
return NewLink(factory.SelfUrlString(entity.GetId()))
}
func (factory *BasicLinkFactory) Links(entity LinkEntity) rest_model.Links {
return rest_model.Links{
EntityNameSelf: factory.SelfLink(entity),
}
}
func (factory BasicLinkFactory) NewNestedLink(entity LinkEntity, elem ...string) rest_model.Link {
elem = append([]string{factory.SelfUrlString(entity.GetId())}, elem...)
//path.Join will remove the ./ prefix in its "clean" operation
return NewLink("./" + path.Join(elem...))
}
func (factory *BasicLinkFactory) EntityName() string {
return factory.entityName
}
type LinkEntity interface {
GetId() string
}
func ToEntityRef(name string, entity LinkEntity, factory LinksFactory) *rest_model.EntityRef {
return &rest_model.EntityRef{
Links: factory.Links(entity),
Entity: factory.EntityName(),
ID: entity.GetId(),
Name: name,
}
}
func NewLink(path string) rest_model.Link {
href := strfmt.URI(path)
return rest_model.Link{
Href: &href,
}
}