Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 544c270

Browse files
authored
refactor: Move code for (de)serialization from/to JSON to model structs (#376)
* refactor: Move code for (de)serialization from/to JSON to model structs Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com> * added comments to the exported methods Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com> * remove unnecessary check Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
1 parent 5626bf9 commit 544c270

40 files changed

+600
-161
lines changed

pkg/api/models/approval.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
// Approval approval
46
type Approval struct {
57

@@ -18,3 +20,18 @@ type Approval struct {
1820
// Time of the event
1921
Time string `json:"time,omitempty"`
2022
}
23+
24+
// ToJSON converts object to JSON string
25+
func (a *Approval) ToJSON() ([]byte, error) {
26+
return json.Marshal(a)
27+
}
28+
29+
// FromJSON converts JSON string to object
30+
func (a *Approval) FromJSON(b []byte) error {
31+
var res Approval
32+
if err := json.Unmarshal(b, &res); err != nil {
33+
return err
34+
}
35+
*a = res
36+
return nil
37+
}

pkg/api/models/create_project.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
// CreateProject create project
46
type CreateProject struct {
57

@@ -20,3 +22,18 @@ type CreateProject struct {
2022
// Required: true
2123
Shipyard *string `json:"shipyard"`
2224
}
25+
26+
// ToJSON converts object to JSON string
27+
func (c *CreateProject) ToJSON() ([]byte, error) {
28+
return json.Marshal(c)
29+
}
30+
31+
// FromJSON converts JSON string to object
32+
func (c *CreateProject) FromJSON(b []byte) error {
33+
var res CreateProject
34+
if err := json.Unmarshal(b, &res); err != nil {
35+
return err
36+
}
37+
*c = res
38+
return nil
39+
}

pkg/api/models/create_service.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package models
22

3+
import "encoding/json"
4+
35
// CreateService create service
46
type CreateService struct {
57

68
// service name
79
// Required: true
810
ServiceName *string `json:"serviceName"`
911
}
12+
13+
// ToJSON converts object to JSON string
14+
func (c *CreateService) ToJSON() ([]byte, error) {
15+
return json.Marshal(c)
16+
}
17+
18+
// FromJSON converts JSON string to object
19+
func (c *CreateService) FromJSON(b []byte) error {
20+
var res CreateService
21+
if err := json.Unmarshal(b, &res); err != nil {
22+
return err
23+
}
24+
*c = res
25+
return nil
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
package models
22

3+
import "encoding/json"
4+
35
// DeleteProjectResponse delete project response
46
type DeleteProjectResponse struct {
57

68
// message
79
Message string `json:"message,omitempty"`
810
}
11+
12+
// ToJSON converts object to JSON string
13+
func (d *DeleteProjectResponse) ToJSON() ([]byte, error) {
14+
return json.Marshal(d)
15+
}
16+
17+
// FromJSON converts JSON string to object
18+
func (d *DeleteProjectResponse) FromJSON(b []byte) error {
19+
var res DeleteProjectResponse
20+
if err := json.Unmarshal(b, &res); err != nil {
21+
return err
22+
}
23+
*d = res
24+
return nil
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
package models
22

3+
import "encoding/json"
4+
35
// DeleteServiceResponse delete service response
46
type DeleteServiceResponse struct {
57

68
// message
79
Message string `json:"message,omitempty"`
810
}
11+
12+
// ToJSON converts object to JSON string
13+
func (d *DeleteServiceResponse) ToJSON() ([]byte, error) {
14+
return json.Marshal(d)
15+
}
16+
17+
// FromJSON converts JSON string to object
18+
func (d *DeleteServiceResponse) FromJSON(b []byte) error {
19+
var res DeleteServiceResponse
20+
if err := json.Unmarshal(b, &res); err != nil {
21+
return err
22+
}
23+
*d = res
24+
return nil
25+
}

pkg/api/models/error.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
// Error error
46
type Error struct {
57

@@ -17,3 +19,18 @@ func (e Error) GetMessage() string {
1719
}
1820
return *e.Message
1921
}
22+
23+
// ToJSON converts object to JSON string
24+
func (e *Error) ToJSON() ([]byte, error) {
25+
return json.Marshal(e)
26+
}
27+
28+
// FromJSON converts JSON string to object
29+
func (e *Error) FromJSON(b []byte) error {
30+
var res Error
31+
if err := json.Unmarshal(b, &res); err != nil {
32+
return err
33+
}
34+
*e = res
35+
return nil
36+
}

pkg/api/models/evaluation.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
type Evaluation struct {
46

57
// Evaluation start timestamp
@@ -14,3 +16,18 @@ type Evaluation struct {
1416
// Evaluation end timestamp
1517
End string `json:"end,omitempty"`
1618
}
19+
20+
// ToJSON converts object to JSON string
21+
func (e *Evaluation) ToJSON() ([]byte, error) {
22+
return json.Marshal(e)
23+
}
24+
25+
// FromJSON converts JSON string to object
26+
func (e *Evaluation) FromJSON(b []byte) error {
27+
var res Evaluation
28+
if err := json.Unmarshal(b, &res); err != nil {
29+
return err
30+
}
31+
*e = res
32+
return nil
33+
}

pkg/api/models/event_context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package models
22

3+
import "encoding/json"
4+
35
// EventContext event context
46
type EventContext struct {
57

68
// keptn context
79
// Required: true
810
KeptnContext *string `json:"keptnContext"`
911
}
12+
13+
// ToJSON converts object to JSON string
14+
func (ec *EventContext) ToJSON() ([]byte, error) {
15+
return json.Marshal(ec)
16+
}
17+
18+
// FromJSON converts JSON string to object
19+
func (ec *EventContext) FromJSON(b []byte) error {
20+
var res EventContext
21+
if err := json.Unmarshal(b, &res); err != nil {
22+
return err
23+
}
24+
*ec = res
25+
return nil
26+
}

pkg/api/models/event_context_info.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
// EventContextInfo event context info
46
type EventContextInfo struct {
57

@@ -12,3 +14,18 @@ type EventContextInfo struct {
1214
// Time of the event
1315
Time string `json:"time,omitempty"`
1416
}
17+
18+
// ToJSON converts object to JSON string
19+
func (ec *EventContextInfo) ToJSON() ([]byte, error) {
20+
return json.Marshal(ec)
21+
}
22+
23+
// FromJSON converts JSON string to object
24+
func (ec *EventContextInfo) FromJSON(b []byte) error {
25+
var res EventContextInfo
26+
if err := json.Unmarshal(b, &res); err != nil {
27+
return err
28+
}
29+
*ec = res
30+
return nil
31+
}

pkg/api/models/events.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import "encoding/json"
4+
35
// Events events
46
type Events struct {
57

@@ -15,3 +17,18 @@ type Events struct {
1517
// Total number of resources
1618
TotalCount float64 `json:"totalCount,omitempty"`
1719
}
20+
21+
// ToJSON converts object to JSON string
22+
func (e *Events) ToJSON() ([]byte, error) {
23+
return json.Marshal(e)
24+
}
25+
26+
// FromJSON converts JSON string to object
27+
func (e *Events) FromJSON(b []byte) error {
28+
var res Events
29+
if err := json.Unmarshal(b, &res); err != nil {
30+
return err
31+
}
32+
*e = res
33+
return nil
34+
}

0 commit comments

Comments
 (0)