Skip to content

Commit

Permalink
Merge pull request #84 from Financial-Times/fix/UPPSF-5624-validate-a…
Browse files Browse the repository at this point in the history
…nnotations-schema-in-pac-before-sending-to-publish

Match the schema in the publishing cluster
  • Loading branch information
angelraynovft authored Oct 7, 2024
2 parents 2126d4d + 91f5d5b commit 67a5a2d
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 67a5a2d

Please sign in to comment.