Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/topup-ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/json"
"strings"
"sync"
)

Expand All @@ -26,6 +27,9 @@ func UpdateTopupGroupRatioByJSONString(jsonStr string) error {
topupGroupRatioMutex.Lock()
defer topupGroupRatioMutex.Unlock()
topupGroupRatio = make(map[string]float64)
if strings.TrimSpace(jsonStr) == "" {
return nil
}
return json.Unmarshal([]byte(jsonStr), &topupGroupRatio)
}

Expand Down
5 changes: 5 additions & 0 deletions setting/auto_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package setting

import (
"strings"

"github.com/QuantumNous/new-api/common"
)

Expand All @@ -21,6 +23,9 @@ func ContainsAutoGroup(group string) bool {

func UpdateAutoGroupsByJsonString(jsonString string) error {
autoGroups = make([]string, 0)
if strings.TrimSpace(jsonString) == "" {
return nil
}
return common.Unmarshal([]byte(jsonString), &autoGroups)
}

Expand Down
4 changes: 4 additions & 0 deletions setting/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package setting

import (
"encoding/json"
"strings"

"github.com/QuantumNous/new-api/common"
)
Expand Down Expand Up @@ -41,6 +42,9 @@ var Chats = []map[string]string{

func UpdateChatsByJsonString(jsonString string) error {
Chats = make([]map[string]string, 0)
if strings.TrimSpace(jsonString) == "" {
return nil
}
return json.Unmarshal([]byte(jsonString), &Chats)
}

Expand Down
5 changes: 5 additions & 0 deletions setting/operation_setting/payment_setting_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This file is the old version of the payment settings file. If you need to add ne
package operation_setting

import (
"strings"

"github.com/QuantumNous/new-api/common"
)

Expand Down Expand Up @@ -38,6 +40,9 @@ var PayMethods = []map[string]string{

func UpdatePayMethodsByJsonString(jsonString string) error {
PayMethods = make([]map[string]string, 0)
if strings.TrimSpace(jsonString) == "" {
return nil
}
return common.Unmarshal([]byte(jsonString), &PayMethods)
}

Expand Down
4 changes: 4 additions & 0 deletions setting/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math"
"strings"
"sync"

"github.com/QuantumNous/new-api/common"
Expand Down Expand Up @@ -32,6 +33,9 @@ func UpdateModelRequestRateLimitGroupByJSONString(jsonStr string) error {
defer ModelRequestRateLimitMutex.RUnlock()

ModelRequestRateLimitGroup = make(map[string][2]int)
if strings.TrimSpace(jsonStr) == "" {
return nil
}
return json.Unmarshal([]byte(jsonStr), &ModelRequestRateLimitGroup)
}

Expand Down
4 changes: 4 additions & 0 deletions setting/user_usable_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package setting

import (
"encoding/json"
"strings"
"sync"

"github.com/QuantumNous/new-api/common"
Expand Down Expand Up @@ -40,6 +41,9 @@ func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
defer userUsableGroupsMutex.Unlock()

userUsableGroups = make(map[string]string)
if strings.TrimSpace(jsonStr) == "" {
return nil
}
return json.Unmarshal([]byte(jsonStr), &userUsableGroups)
}

Expand Down
14 changes: 14 additions & 0 deletions types/rw_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"strings"
"sync"

"github.com/QuantumNous/new-api/common"
Expand Down Expand Up @@ -78,6 +79,11 @@ func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) err
m.mutex.Lock()
defer m.mutex.Unlock()
m.data = make(map[K]V)
// Empty/blank value means "no entries"; treat as an empty map instead of
// failing with "unexpected end of JSON input".
if strings.TrimSpace(jsonStr) == "" {
return nil
}
return common.Unmarshal([]byte(jsonStr), &m.data)
}

Expand All @@ -86,6 +92,14 @@ func LoadFromJsonStringWithCallback[K comparable, V any](m *RWMap[K, V], jsonStr
m.mutex.Lock()
defer m.mutex.Unlock()
m.data = make(map[K]V)
// Empty/blank value means "no entries"; treat as an empty map instead of
// failing with "unexpected end of JSON input".
if strings.TrimSpace(jsonStr) == "" {
if onSuccess != nil {
onSuccess()
}
return nil
}
err := common.Unmarshal([]byte(jsonStr), &m.data)
if err == nil && onSuccess != nil {
onSuccess()
Expand Down
42 changes: 42 additions & 0 deletions types/rw_map_empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import "testing"

// Empty/blank option values must be treated as "no entries" rather than
// producing "unexpected end of JSON input" during option sync.
func TestLoadFromJsonStringEmpty(t *testing.T) {
for _, in := range []string{"", " ", "\n\t "} {
m := NewRWMap[string, float64]()
if err := LoadFromJsonString(m, in); err != nil {
t.Fatalf("LoadFromJsonString(%q) returned error: %v", in, err)
}
if got := m.Len(); got != 0 {
t.Fatalf("LoadFromJsonString(%q) expected empty map, got len=%d", in, got)
}
}
}

func TestLoadFromJsonStringWithCallbackEmpty(t *testing.T) {
called := false
m := NewRWMap[string, float64]()
if err := LoadFromJsonStringWithCallback(m, "", func() { called = true }); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Fatal("expected onSuccess callback to be invoked for empty input")
}
if m.Len() != 0 {
t.Fatalf("expected empty map, got len=%d", m.Len())
}
}

// Non-empty valid JSON must still load normally.
func TestLoadFromJsonStringValid(t *testing.T) {
m := NewRWMap[string, float64]()
if err := LoadFromJsonString(m, `{"a":1.5}`); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if v, ok := m.Get("a"); !ok || v != 1.5 {
t.Fatalf("expected a=1.5, got %v ok=%v", v, ok)
}
}