Skip to content

Commit

Permalink
OPENAPI: these columns and such are actually int pointers, so we have…
Browse files Browse the repository at this point in the history
… to dereference them
  • Loading branch information
jacobm-splunk committed Jul 29, 2024
1 parent f0bccc1 commit a841451
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions model/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit a841451

Please sign in to comment.