submissions: centralize hashing and formatting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Quaternions 2024-12-17 15:57:29 -08:00
parent 8c45736cf4
commit 6bab1e1b6b
3 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,19 @@
package model package model
import "time" import (
"fmt"
"time"
"github.com/dchest/siphash"
)
func HashSource(source string) uint64{
return siphash.Hash(0, 0, []byte(source))
}
func HashFormat(hash uint64) string{
return fmt.Sprintf("%016x", hash)
}
type Script struct { type Script struct {
ID int64 `gorm:"primaryKey"` ID int64 `gorm:"primaryKey"`

View File

@ -2,7 +2,6 @@ package service
import ( import (
"context" "context"
"fmt"
"strconv" "strconv"
"git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/api"
@ -74,7 +73,7 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, request *api.ListScrip
for i := 0; i < len(items); i++ { for i := 0; i < len(items); i++ {
resp = append(resp, api.ScriptPolicy{ resp = append(resp, api.ScriptPolicy{
ID: items[i].ID, ID: items[i].ID,
FromScriptHash: fmt.Sprintf("%x", items[i].FromScriptHash), FromScriptHash: model.HashFormat(items[i].FromScriptHash),
ToScriptID: items[i].ToScriptID, ToScriptID: items[i].ToScriptID,
Policy: int32(items[i].Policy), Policy: int32(items[i].Policy),
}) })
@ -121,7 +120,7 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol
return &api.ScriptPolicy{ return &api.ScriptPolicy{
ID: policy.ID, ID: policy.ID,
FromScriptHash: fmt.Sprintf("%x", policy.FromScriptHash), FromScriptHash: model.HashFormat(policy.FromScriptHash),
ToScriptID: policy.ToScriptID, ToScriptID: policy.ToScriptID,
Policy: int32(policy.Policy), Policy: int32(policy.Policy),
}, nil }, nil
@ -153,7 +152,7 @@ func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetS
return &api.ScriptPolicy{ return &api.ScriptPolicy{
ID: policy.ID, ID: policy.ID,
FromScriptHash: fmt.Sprintf("%x", policy.FromScriptHash), FromScriptHash: model.HashFormat(policy.FromScriptHash),
ToScriptID: policy.ToScriptID, ToScriptID: policy.ToScriptID,
Policy: int32(policy.Policy), Policy: int32(policy.Policy),
}, nil }, nil

View File

@ -2,12 +2,10 @@ package service
import ( import (
"context" "context"
"fmt"
"git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/api"
"git.itzana.me/strafesnet/maps-service/pkg/datastore" "git.itzana.me/strafesnet/maps-service/pkg/datastore"
"git.itzana.me/strafesnet/maps-service/pkg/model" "git.itzana.me/strafesnet/maps-service/pkg/model"
"github.com/dchest/siphash"
) )
// CreateScript implements createScript operation. // CreateScript implements createScript operation.
@ -28,7 +26,7 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a
script, err := svc.DB.Scripts().Create(ctx, model.Script{ script, err := svc.DB.Scripts().Create(ctx, model.Script{
ID: 0, ID: 0,
Name: req.Name, Name: req.Name,
Hash: siphash.Hash(0, 0, []byte(req.Source)), Hash: model.HashSource(req.Source),
Source: req.Source, Source: req.Source,
SubmissionID: req.SubmissionID.Or(0), SubmissionID: req.SubmissionID.Or(0),
}) })
@ -80,7 +78,7 @@ func (svc *Service) GetScript(ctx context.Context, params api.GetScriptParams) (
return &api.Script{ return &api.Script{
ID: script.ID, ID: script.ID,
Name: script.Name, Name: script.Name,
Hash: fmt.Sprintf("%x", script.Hash), Hash: model.HashFormat(script.Hash),
Source: script.Source, Source: script.Source,
SubmissionID: script.SubmissionID, SubmissionID: script.SubmissionID,
}, nil }, nil
@ -107,7 +105,7 @@ func (svc *Service) UpdateScript(ctx context.Context, req *api.ScriptUpdate, par
} }
if source, ok := req.Source.Get(); ok { if source, ok := req.Source.Get(); ok {
pmap.Add("source", source) pmap.Add("source", source)
pmap.Add("hash", siphash.Hash(0, 0, []byte(source))) pmap.Add("hash", model.HashSource(source))
} }
if SubmissionID, ok := req.SubmissionID.Get(); ok { if SubmissionID, ok := req.SubmissionID.Get(); ok {
pmap.Add("submission_id", SubmissionID) pmap.Add("submission_id", SubmissionID)