generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services_serializers.go
56 lines (48 loc) · 1.78 KB
/
services_serializers.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
package v1serializers
import (
"github.com/broadinstitute/sherlock/internal/models/v1models"
)
// builds_serializers.go contains logic for building
// http responses from the builds data base model while avoiding
// dependencies on the database model in the route handling logic directly.
// This is an essentially an abstraction layer to give use more control over
// what is returned from api endpoints
// ServiceResponse is the type used to serialize Service data that is returned
// to clients of the sherlock api
type ServiceResponse struct {
ID int `json:"id"`
Name string `json:"name"`
RepoURL string `json:"repo_url"`
}
// ServiceSerializer is used to serializer a single Service model
// to a used to generate responses from the /services api group
type ServiceSerializer struct {
Service v1models.Service
}
// Response takes a Service Model entity and transforms it into a ServiceResponse
func (ss *ServiceSerializer) Response() ServiceResponse {
return ServiceResponse{
ID: ss.Service.ID,
Name: ss.Service.Name,
RepoURL: ss.Service.RepoURL,
}
}
// ServicesSerializer is used to serialize a ServiceModel
// entity to a a format used in http response bodies
type ServicesSerializer struct {
Services []v1models.Service
}
// Response serializes Service models into Service Responses
func (ss *ServicesSerializer) Response() []ServiceResponse {
services := []ServiceResponse{}
for _, service := range ss.Services {
serializer := ServiceSerializer{service}
services = append(services, serializer.Response())
}
return services
}
// Response is a type that allows all data returned from the /service api group to share a consistent structure
type Response struct {
Services []ServiceResponse `json:"services"`
Error string `json:"error,omitempty"`
}