Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
revert: config.go
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 committed Oct 20, 2024
1 parent 94b2d90 commit afeea0c
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions internal/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@ import (
"github.com/s0up4200/redactedhook/internal/config"
)

type configField struct {
webhookField interface{}
configValue interface{}
}

// fallbackToConfig prioritizes webhook data over config data.
// If webhook data is present, it overwrites the existing config data.
func fallbackToConfig(requestData *RequestData) {
cfg := config.GetConfig()

fields := []configField{
{&requestData.REDUserID, cfg.UserIDs.REDUserID},
{&requestData.OPSUserID, cfg.UserIDs.OPSUserID},
{&requestData.REDKey, cfg.IndexerKeys.REDKey},
{&requestData.OPSKey, cfg.IndexerKeys.OPSKey},
{&requestData.MinRatio, cfg.Ratio.MinRatio},
{&requestData.MinSize, cfg.ParsedSizes.MinSize},
{&requestData.MaxSize, cfg.ParsedSizes.MaxSize},
{&requestData.Uploaders, cfg.Uploaders.Uploaders},
{&requestData.Mode, cfg.Uploaders.Mode},
{&requestData.RecordLabel, cfg.RecordLabels.RecordLabels},
// Helper functions to set fields, prioritizing webhook data if present
setInt := func(webhookField *int, configValue int) {
if *webhookField == 0 {
*webhookField = configValue
}
}

setFloat64 := func(webhookField *float64, configValue float64) {
if *webhookField == 0 {
*webhookField = configValue
}
}

for _, field := range fields {
switch v := field.webhookField.(type) {
case *int:
if *v == 0 {
*v = field.configValue.(int)
}
case *float64:
if *v == 0 {
*v = field.configValue.(float64)
}
case *bytesize.ByteSize:
if *v == 0 {
*v = field.configValue.(bytesize.ByteSize)
}
case *string:
if *v == "" {
*v = field.configValue.(string)
}
setByteSize := func(webhookField *bytesize.ByteSize, configValue bytesize.ByteSize) {
if *webhookField == 0 {
*webhookField = configValue
}
}

setString := func(webhookField *string, configValue string) {
if *webhookField == "" {
*webhookField = configValue
}
}

// Check and set the fields, ensuring webhook data takes priority if present
setInt(&requestData.REDUserID, cfg.UserIDs.REDUserID)
setInt(&requestData.OPSUserID, cfg.UserIDs.OPSUserID)
setString(&requestData.REDKey, cfg.IndexerKeys.REDKey)
setString(&requestData.OPSKey, cfg.IndexerKeys.OPSKey)
setFloat64(&requestData.MinRatio, cfg.Ratio.MinRatio)
setByteSize(&requestData.MinSize, cfg.ParsedSizes.MinSize)
setByteSize(&requestData.MaxSize, cfg.ParsedSizes.MaxSize)
setString(&requestData.Uploaders, cfg.Uploaders.Uploaders)
setString(&requestData.Mode, cfg.Uploaders.Mode)
setString(&requestData.RecordLabel, cfg.RecordLabels.RecordLabels)
}

0 comments on commit afeea0c

Please sign in to comment.