Skip to content

Commit

Permalink
Match the schema in the publishing cluster
Browse files Browse the repository at this point in the history
We are introducing an annotations body struct which helps us
match the schema in the publishing cluster. This way we can filter the required fields and avoid validation errors.
  • Loading branch information
angelraynovft committed Oct 4, 2024
1 parent 2126d4d commit 91f5d5b
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions synchronise/synchronise.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const (
FTPinkPublication = "88fdde6c-2aa4-4f78-af02-9f680097cfd6"
)

type AnnotationsBody struct {
Annotations []Annotation `json:"annotations"`
Publication []string `json:"publication"`
}

type Annotation struct {
ID string `json:"id"`
Predicate string `json:"predicate"`
}

type API struct {
client *http.Client
username string
Expand Down Expand Up @@ -57,7 +67,7 @@ func (api *API) SyncWithPublishingCluster(req *http.Request) error {
// Restore the io.ReadCloser after reading from it
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

bodyBytes, err = addPublicationToBody(bodyBytes)
bodyBytes, err = prepareAnnotationsBody(bodyBytes)
if err != nil {
return err
}
Expand Down Expand Up @@ -106,24 +116,25 @@ func (api *API) SyncWithPublishingCluster(req *http.Request) error {
return nil
}

func addPublicationToBody(bodyBytes []byte) ([]byte, error) {
// Decode the body into a map
var bodyMap map[string]interface{}
err := json.Unmarshal(bodyBytes, &bodyMap)
func prepareAnnotationsBody(bodyBytes []byte) ([]byte, error) {
// Decode the body into an AnnotationsBody struct which matches the expected schema in publishing cluster
var ann AnnotationsBody
err := json.Unmarshal(bodyBytes, &ann)
if err != nil {
return nil, err
}

// Check if the map contains a key for "publication"
if _, ok := bodyMap["publication"]; !ok {
// Add publication so we pass the validation in publishing cluster
if ann.Publication == nil {
// If not, add it
bodyMap["publication"] = []string{FTPinkPublication}
ann.Publication = []string{FTPinkPublication}
}

// Re-encode the body into JSON
bodyBytes, err = json.Marshal(bodyMap)
if err != nil {
return nil, err
}
// Re-encode the body into JSON
bodyBytes, err = json.Marshal(ann)
if err != nil {
return nil, err
}

return bodyBytes, nil
}

0 comments on commit 91f5d5b

Please sign in to comment.