Skip to content

Commit 7f41d5d

Browse files
committed
renamed Priority to Score in MessageAliases
1 parent 7b44795 commit 7f41d5d

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

.github/templates/README.template.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,28 +230,29 @@ example:
230230

231231
To improve compatibility with other services Secured Signal API provides aliases for the `message` attribute by default:
232232

233-
| Alias | Priority |
234-
| ----------- | -------- |
235-
| msg | 100 |
236-
| content | 99 |
237-
| description | 98 |
238-
| text | 20 |
239-
| body | 15 |
240-
| summary | 10 |
241-
| details | 9 |
242-
| payload | 2 |
243-
| data | 1 |
244-
245-
Secured Signal API will use the highest priority Message Alias to extract the correct message from the Request Body.
246-
247-
Message Aliases can be added by setting `MESSAGE_ALIASES`:
233+
| Alias | Score |
234+
| ----------- | ----- |
235+
| msg | 100 |
236+
| content | 99 |
237+
| description | 98 |
238+
| text | 20 |
239+
| body | 15 |
240+
| summary | 10 |
241+
| details | 9 |
242+
| payload | 2 |
243+
| data | 1 |
244+
245+
Secured Signal API will pick the best scoring Message Alias (if available) to extract the correct message from the Request Body.
246+
247+
Message Aliases can be added by setting `MESSAGE_ALIASES` to a valid json array containing dictionaries of `alias`, the json key to be used for lookup (use `.` dots for using values from a nested dictionary and `[i]` to get values from an array):
248248

249249
```yaml
250250
environment:
251251
MESSAGE_ALIASES: |
252252
[
253-
{ "alias": "note", "priority": 4 },
254-
{ "alias": "test", "priority": 3 }
253+
{ "alias": "msg", "score": 80 },
254+
{ "alias": "data.message", "score": 79 },
255+
{ "alias": "array[0].message", "score": 78 },
255256
]
256257
```
257258

internals/proxy/middlewares/body.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"net/http"
77
"strconv"
88

9+
"github.com/codeshelldev/secured-signal-api/utils"
910
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1011
request "github.com/codeshelldev/secured-signal-api/utils/request"
1112
)
1213

1314
type MessageAlias struct {
1415
Alias string
15-
Priority int
16+
Score int
1617
}
1718

1819
type BodyMiddleware struct {
@@ -74,17 +75,28 @@ func getMessage(aliases []MessageAlias, data map[string]interface{}) (string, ma
7475
var best int
7576

7677
for _, alias := range aliases {
77-
aliasKey := alias.Alias
78-
priority := alias.Priority
78+
aliasValue, score, ok := processAlias(alias, data)
7979

80-
value, ok := data[aliasKey]
81-
82-
if ok && value != "" && priority > best {
83-
content = data[aliasKey].(string)
80+
if ok && score > best {
81+
content = aliasValue
8482
}
8583

86-
data[aliasKey] = nil
84+
data[alias.Alias] = nil
8785
}
8886

8987
return content, data
88+
}
89+
90+
func processAlias(alias MessageAlias, data map[string]interface{}) (string, int, bool) {
91+
aliasKey := alias.Alias
92+
93+
value, ok := utils.GetJsonByPath(aliasKey, data)
94+
95+
aliasValue, isStr := value.(string)
96+
97+
if isStr && ok && aliasValue != "" {
98+
return aliasValue, alias.Score, true
99+
} else {
100+
return "", 0, false
101+
}
90102
}

0 commit comments

Comments
 (0)