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

Commit

Permalink
refactor: Move code for (de)serialization from/to JSON to model struc…
Browse files Browse the repository at this point in the history
…ts (#376)

* refactor: Move code for (de)serialization from/to JSON to model structs

Signed-off-by: odubajDT <[email protected]>

* added comments to the exported methods

Signed-off-by: odubajDT <[email protected]>

* remove unnecessary check

Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Jan 12, 2022
1 parent 5626bf9 commit 544c270
Show file tree
Hide file tree
Showing 40 changed files with 600 additions and 161 deletions.
17 changes: 17 additions & 0 deletions pkg/api/models/approval.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// Approval approval
type Approval struct {

Expand All @@ -18,3 +20,18 @@ type Approval struct {
// Time of the event
Time string `json:"time,omitempty"`
}

// ToJSON converts object to JSON string
func (a *Approval) ToJSON() ([]byte, error) {
return json.Marshal(a)
}

// FromJSON converts JSON string to object
func (a *Approval) FromJSON(b []byte) error {
var res Approval
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*a = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/create_project.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// CreateProject create project
type CreateProject struct {

Expand All @@ -20,3 +22,18 @@ type CreateProject struct {
// Required: true
Shipyard *string `json:"shipyard"`
}

// ToJSON converts object to JSON string
func (c *CreateProject) ToJSON() ([]byte, error) {
return json.Marshal(c)
}

// FromJSON converts JSON string to object
func (c *CreateProject) FromJSON(b []byte) error {
var res CreateProject
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*c = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/create_service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package models

import "encoding/json"

// CreateService create service
type CreateService struct {

// service name
// Required: true
ServiceName *string `json:"serviceName"`
}

// ToJSON converts object to JSON string
func (c *CreateService) ToJSON() ([]byte, error) {
return json.Marshal(c)
}

// FromJSON converts JSON string to object
func (c *CreateService) FromJSON(b []byte) error {
var res CreateService
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*c = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/delete_project_response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package models

import "encoding/json"

// DeleteProjectResponse delete project response
type DeleteProjectResponse struct {

// message
Message string `json:"message,omitempty"`
}

// ToJSON converts object to JSON string
func (d *DeleteProjectResponse) ToJSON() ([]byte, error) {
return json.Marshal(d)
}

// FromJSON converts JSON string to object
func (d *DeleteProjectResponse) FromJSON(b []byte) error {
var res DeleteProjectResponse
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*d = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/delete_service_response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package models

import "encoding/json"

// DeleteServiceResponse delete service response
type DeleteServiceResponse struct {

// message
Message string `json:"message,omitempty"`
}

// ToJSON converts object to JSON string
func (d *DeleteServiceResponse) ToJSON() ([]byte, error) {
return json.Marshal(d)
}

// FromJSON converts JSON string to object
func (d *DeleteServiceResponse) FromJSON(b []byte) error {
var res DeleteServiceResponse
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*d = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// Error error
type Error struct {

Expand All @@ -17,3 +19,18 @@ func (e Error) GetMessage() string {
}
return *e.Message
}

// ToJSON converts object to JSON string
func (e *Error) ToJSON() ([]byte, error) {
return json.Marshal(e)
}

// FromJSON converts JSON string to object
func (e *Error) FromJSON(b []byte) error {
var res Error
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*e = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/evaluation.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

type Evaluation struct {

// Evaluation start timestamp
Expand All @@ -14,3 +16,18 @@ type Evaluation struct {
// Evaluation end timestamp
End string `json:"end,omitempty"`
}

// ToJSON converts object to JSON string
func (e *Evaluation) ToJSON() ([]byte, error) {
return json.Marshal(e)
}

// FromJSON converts JSON string to object
func (e *Evaluation) FromJSON(b []byte) error {
var res Evaluation
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*e = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/event_context.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package models

import "encoding/json"

// EventContext event context
type EventContext struct {

// keptn context
// Required: true
KeptnContext *string `json:"keptnContext"`
}

// ToJSON converts object to JSON string
func (ec *EventContext) ToJSON() ([]byte, error) {
return json.Marshal(ec)
}

// FromJSON converts JSON string to object
func (ec *EventContext) FromJSON(b []byte) error {
var res EventContext
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*ec = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/event_context_info.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// EventContextInfo event context info
type EventContextInfo struct {

Expand All @@ -12,3 +14,18 @@ type EventContextInfo struct {
// Time of the event
Time string `json:"time,omitempty"`
}

// ToJSON converts object to JSON string
func (ec *EventContextInfo) ToJSON() ([]byte, error) {
return json.Marshal(ec)
}

// FromJSON converts JSON string to object
func (ec *EventContextInfo) FromJSON(b []byte) error {
var res EventContextInfo
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*ec = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// Events events
type Events struct {

Expand All @@ -15,3 +17,18 @@ type Events struct {
// Total number of resources
TotalCount float64 `json:"totalCount,omitempty"`
}

// ToJSON converts object to JSON string
func (e *Events) ToJSON() ([]byte, error) {
return json.Marshal(e)
}

// FromJSON converts JSON string to object
func (e *Events) FromJSON(b []byte) error {
var res Events
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*e = res
return nil
}
15 changes: 15 additions & 0 deletions pkg/api/models/keptn_context_extended_c_e.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,18 @@ func (ce *KeptnContextExtendedCE) GetTemporaryData(key string, tmpdata interface
}
return fmt.Errorf("temporary data with key %s not found", key)
}

// ToJSON converts object to JSON string
func (ce *KeptnContextExtendedCE) ToJSON() ([]byte, error) {
return json.Marshal(ce)
}

// FromJSON converts JSON string to object
func (ce *KeptnContextExtendedCE) FromJSON(b []byte) error {
var res KeptnContextExtendedCE
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*ce = res
return nil
}
50 changes: 49 additions & 1 deletion pkg/api/models/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import "time"
import (
"encoding/json"
"time"
)

type LogEntry struct {
IntegrationID string `json:"integrationid" bson:"integrationid"`
Expand Down Expand Up @@ -34,3 +37,48 @@ type CreateLogsRequest struct {
// logs
Logs []LogEntry `form:"logs" json:"logs"`
}

// ToJSON converts object to JSON string
func (l *LogEntry) ToJSON() ([]byte, error) {
return json.Marshal(l)
}

// FromJSON converts JSON string to object
func (l *LogEntry) FromJSON(b []byte) error {
var res LogEntry
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*l = res
return nil
}

// ToJSON converts object to JSON string
func (l *GetLogsResponse) ToJSON() ([]byte, error) {
return json.Marshal(l)
}

// FromJSON converts JSON string to object
func (l *GetLogsResponse) FromJSON(b []byte) error {
var res GetLogsResponse
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*l = res
return nil
}

// ToJSON converts object to JSON string
func (l *CreateLogsRequest) ToJSON() ([]byte, error) {
return json.Marshal(l)
}

// FromJSON converts JSON string to object
func (l *CreateLogsRequest) FromJSON(b []byte) error {
var res CreateLogsRequest
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*l = res
return nil
}
17 changes: 17 additions & 0 deletions pkg/api/models/metadata.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "encoding/json"

// Metadata metadata
type Metadata struct {

Expand All @@ -18,3 +20,18 @@ type Metadata struct {
// namespace
Namespace string `json:"namespace,omitempty"`
}

// ToJSON converts object to JSON string
func (m *Metadata) ToJSON() ([]byte, error) {
return json.Marshal(m)
}

// FromJSON converts JSON string to object
func (m *Metadata) FromJSON(b []byte) error {
var res Metadata
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*m = res
return nil
}
Loading

0 comments on commit 544c270

Please sign in to comment.