From a8414513a2a77139bc1e69cfb938f9401205f649 Mon Sep 17 00:00:00 2001 From: Jacob Moore Date: Mon, 29 Jul 2024 19:49:57 -0400 Subject: [PATCH] OPENAPI: these columns and such are actually int pointers, so we have to dereference them --- model/report.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/model/report.go b/model/report.go index 2de6fc6..4833904 100644 --- a/model/report.go +++ b/model/report.go @@ -4,11 +4,64 @@ package model import ( + "crypto/sha256" + "encoding/base64" + "encoding/json" + "fmt" "github.com/pb33f/libopenapi/what-changed/model" "github.com/pb33f/libopenapi/what-changed/reports" "time" ) +type HashedChange struct { + *model.Change + ChangeHash string `json:"changeHash,omitempty"` +} + +func (hc *HashedChange) MarshalJSON() ([]byte, error) { + changeJson, err := hc.Change.MarshalJSON() + + if err != nil { + return nil, err + } + + var data map[string]interface{} + + err = json.Unmarshal(changeJson, &data) + if err != nil { + return nil, err + } + + data["changeHash"] = hc.ChangeHash + + return json.Marshal(data) +} + +func getIntValue(pointer *int) int { + if pointer == nil { + return -1 + } + + return *pointer +} + +func (hc *HashedChange) HashChange() { + + context := hc.Context + contextString := fmt.Sprintf("%d-%d-%d-%d", + getIntValue(context.OriginalLine), + getIntValue(context.OriginalColumn), + getIntValue(context.NewLine), + getIntValue(context.NewColumn)) + + changeString := fmt.Sprintf("%d-%s-%s-%s-%s", hc.ChangeType, hc.Property, hc.Original, hc.New, contextString) + + hasher := sha256.New() + hasher.Write([]byte(changeString)) + + hc.ChangeHash = base64.URLEncoding.EncodeToString(hasher.Sum(nil)) +} + type Report struct { ID uint `gorm:"primaryKey" json:"-"` Summary map[string]*reports.Changed `gorm:"-" json:"reportSummary"`