From 91f5d5b8f46a27534529b4cf9b3c2eaf8ea5f5ac Mon Sep 17 00:00:00 2001 From: angel raynov Date: Fri, 4 Oct 2024 12:09:44 +0300 Subject: [PATCH] Match the schema in the publishing cluster 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. --- synchronise/synchronise.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/synchronise/synchronise.go b/synchronise/synchronise.go index b96ac6d..ea603ea 100644 --- a/synchronise/synchronise.go +++ b/synchronise/synchronise.go @@ -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 @@ -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 } @@ -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 }