Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
56c1994a
authored
Jan 03, 2026
by
Seefs
Committed by
GitHub
Jan 03, 2026
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2568 from seefs001/feature/channel_override_trim_prefix
parents
be567ef7
552e51c1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
118 additions
and
3 deletions
+118
-3
relay/common/override.go
+118
-3
relay/common/override_test.go
+0
-0
No files found.
relay/common/override.go
View file @
56c1994a
...
...
@@ -23,7 +23,7 @@ type ConditionOperation struct {
type
ParamOperation
struct
{
Path
string
`json:"path"`
Mode
string
`json:"mode"`
// delete, set, move,
prepend, append
Mode
string
`json:"mode"`
// delete, set, move,
copy, prepend, append, trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, regex_replace
Value
interface
{}
`json:"value"`
KeepOrigin
bool
`json:"keep_origin"`
From
string
`json:"from,omitempty"`
...
...
@@ -330,8 +330,6 @@ func applyOperations(jsonStr string, operations []ParamOperation, conditionConte
}
// 处理路径中的负数索引
opPath
:=
processNegativeIndex
(
result
,
op
.
Path
)
opFrom
:=
processNegativeIndex
(
result
,
op
.
From
)
opTo
:=
processNegativeIndex
(
result
,
op
.
To
)
switch
op
.
Mode
{
case
"delete"
:
...
...
@@ -342,11 +340,38 @@ func applyOperations(jsonStr string, operations []ParamOperation, conditionConte
}
result
,
err
=
sjson
.
Set
(
result
,
opPath
,
op
.
Value
)
case
"move"
:
opFrom
:=
processNegativeIndex
(
result
,
op
.
From
)
opTo
:=
processNegativeIndex
(
result
,
op
.
To
)
result
,
err
=
moveValue
(
result
,
opFrom
,
opTo
)
case
"copy"
:
if
op
.
From
==
""
||
op
.
To
==
""
{
return
""
,
fmt
.
Errorf
(
"copy from/to is required"
)
}
opFrom
:=
processNegativeIndex
(
result
,
op
.
From
)
opTo
:=
processNegativeIndex
(
result
,
op
.
To
)
result
,
err
=
copyValue
(
result
,
opFrom
,
opTo
)
case
"prepend"
:
result
,
err
=
modifyValue
(
result
,
opPath
,
op
.
Value
,
op
.
KeepOrigin
,
true
)
case
"append"
:
result
,
err
=
modifyValue
(
result
,
opPath
,
op
.
Value
,
op
.
KeepOrigin
,
false
)
case
"trim_prefix"
:
result
,
err
=
trimStringValue
(
result
,
opPath
,
op
.
Value
,
true
)
case
"trim_suffix"
:
result
,
err
=
trimStringValue
(
result
,
opPath
,
op
.
Value
,
false
)
case
"ensure_prefix"
:
result
,
err
=
ensureStringAffix
(
result
,
opPath
,
op
.
Value
,
true
)
case
"ensure_suffix"
:
result
,
err
=
ensureStringAffix
(
result
,
opPath
,
op
.
Value
,
false
)
case
"trim_space"
:
result
,
err
=
transformStringValue
(
result
,
opPath
,
strings
.
TrimSpace
)
case
"to_lower"
:
result
,
err
=
transformStringValue
(
result
,
opPath
,
strings
.
ToLower
)
case
"to_upper"
:
result
,
err
=
transformStringValue
(
result
,
opPath
,
strings
.
ToUpper
)
case
"replace"
:
result
,
err
=
replaceStringValue
(
result
,
opPath
,
op
.
From
,
op
.
To
)
case
"regex_replace"
:
result
,
err
=
regexReplaceStringValue
(
result
,
opPath
,
op
.
From
,
op
.
To
)
default
:
return
""
,
fmt
.
Errorf
(
"unknown operation: %s"
,
op
.
Mode
)
}
...
...
@@ -369,6 +394,14 @@ func moveValue(jsonStr, fromPath, toPath string) (string, error) {
return
sjson
.
Delete
(
result
,
fromPath
)
}
func
copyValue
(
jsonStr
,
fromPath
,
toPath
string
)
(
string
,
error
)
{
sourceValue
:=
gjson
.
Get
(
jsonStr
,
fromPath
)
if
!
sourceValue
.
Exists
()
{
return
jsonStr
,
fmt
.
Errorf
(
"source path does not exist: %s"
,
fromPath
)
}
return
sjson
.
Set
(
jsonStr
,
toPath
,
sourceValue
.
Value
())
}
func
modifyValue
(
jsonStr
,
path
string
,
value
interface
{},
keepOrigin
,
isPrepend
bool
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
switch
{
...
...
@@ -422,6 +455,88 @@ func modifyString(jsonStr, path string, value interface{}, isPrepend bool) (stri
return
sjson
.
Set
(
jsonStr
,
path
,
newStr
)
}
func
trimStringValue
(
jsonStr
,
path
string
,
value
interface
{},
isPrefix
bool
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
if
current
.
Type
!=
gjson
.
String
{
return
jsonStr
,
fmt
.
Errorf
(
"operation not supported for type: %v"
,
current
.
Type
)
}
if
value
==
nil
{
return
jsonStr
,
fmt
.
Errorf
(
"trim value is required"
)
}
valueStr
:=
fmt
.
Sprintf
(
"%v"
,
value
)
var
newStr
string
if
isPrefix
{
newStr
=
strings
.
TrimPrefix
(
current
.
String
(),
valueStr
)
}
else
{
newStr
=
strings
.
TrimSuffix
(
current
.
String
(),
valueStr
)
}
return
sjson
.
Set
(
jsonStr
,
path
,
newStr
)
}
func
ensureStringAffix
(
jsonStr
,
path
string
,
value
interface
{},
isPrefix
bool
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
if
current
.
Type
!=
gjson
.
String
{
return
jsonStr
,
fmt
.
Errorf
(
"operation not supported for type: %v"
,
current
.
Type
)
}
if
value
==
nil
{
return
jsonStr
,
fmt
.
Errorf
(
"ensure value is required"
)
}
valueStr
:=
fmt
.
Sprintf
(
"%v"
,
value
)
if
valueStr
==
""
{
return
jsonStr
,
fmt
.
Errorf
(
"ensure value is required"
)
}
currentStr
:=
current
.
String
()
if
isPrefix
{
if
strings
.
HasPrefix
(
currentStr
,
valueStr
)
{
return
jsonStr
,
nil
}
return
sjson
.
Set
(
jsonStr
,
path
,
valueStr
+
currentStr
)
}
if
strings
.
HasSuffix
(
currentStr
,
valueStr
)
{
return
jsonStr
,
nil
}
return
sjson
.
Set
(
jsonStr
,
path
,
currentStr
+
valueStr
)
}
func
transformStringValue
(
jsonStr
,
path
string
,
transform
func
(
string
)
string
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
if
current
.
Type
!=
gjson
.
String
{
return
jsonStr
,
fmt
.
Errorf
(
"operation not supported for type: %v"
,
current
.
Type
)
}
return
sjson
.
Set
(
jsonStr
,
path
,
transform
(
current
.
String
()))
}
func
replaceStringValue
(
jsonStr
,
path
,
from
,
to
string
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
if
current
.
Type
!=
gjson
.
String
{
return
jsonStr
,
fmt
.
Errorf
(
"operation not supported for type: %v"
,
current
.
Type
)
}
if
from
==
""
{
return
jsonStr
,
fmt
.
Errorf
(
"replace from is required"
)
}
return
sjson
.
Set
(
jsonStr
,
path
,
strings
.
ReplaceAll
(
current
.
String
(),
from
,
to
))
}
func
regexReplaceStringValue
(
jsonStr
,
path
,
pattern
,
replacement
string
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
if
current
.
Type
!=
gjson
.
String
{
return
jsonStr
,
fmt
.
Errorf
(
"operation not supported for type: %v"
,
current
.
Type
)
}
if
pattern
==
""
{
return
jsonStr
,
fmt
.
Errorf
(
"regex pattern is required"
)
}
re
,
err
:=
regexp
.
Compile
(
pattern
)
if
err
!=
nil
{
return
jsonStr
,
err
}
return
sjson
.
Set
(
jsonStr
,
path
,
re
.
ReplaceAllString
(
current
.
String
(),
replacement
))
}
func
mergeObjects
(
jsonStr
,
path
string
,
value
interface
{},
keepOrigin
bool
)
(
string
,
error
)
{
current
:=
gjson
.
Get
(
jsonStr
,
path
)
var
currentMap
,
newMap
map
[
string
]
interface
{}
...
...
relay/common/override_test.go
0 → 100644
View file @
56c1994a
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment