Replacer

Overview

Replacer is a string replacer resource. Technically speaking, it is a reusable component that provides string and []byte value replacing. It it a reusable resource that is used from other resources.

It is used as, for example,

  • log masking
  • header value modification
  • body modification

Defined replacer types are listed in the table.

Replace typeUsed methodTry on Go Playground
Noop-https://go.dev/play/p/pEaudzyUeOX
Fixed-https://go.dev/play/p/gIBOBblqe4w
Valuestrings#ReplaceAllhttps://go.dev/play/p/Qz5wmj2MKca
Leftstrings#Repeathttps://go.dev/play/p/_XnzkUO5DNE
Rightstrings#Repeathttps://go.dev/play/p/nDmuJ1F-TX2
Trimstrings#Trimhttps://go.dev/play/p/p-O6aMuV_R0
TrimLeftstrings#TrimLefthttps://go.dev/play/p/8tSm5oex608
TrimRightstrings#TrimRighthttps://go.dev/play/p/xuH7DdM0yN_y
TrimPrefixstrings#TrimPrefixhttps://go.dev/play/p/jhIFvHB8FoH
TrimSuffixstrings#TrimSuffixhttps://go.dev/play/p/uY2WNAPlZ9M
EncodeRegexp + Encodehttps://go.dev/play/p/yy1QvqHOjue
HashRegexp + Hashhttps://go.dev/play/p/Hjc8Z2rR1wz
Regexpregexp#Regexp.ReplaceAllhttps://go.dev/play/p/T6Mu9usjstw
Expandregexp#Regexp.Expandhttps://go.dev/play/p/Pw6tLUVZYcO
EncryptRegexp + Encryptionhttps://go.dev/play/p/mBvv3DGInhg

Resource Definition

Matcher is defined in the proto/kernel/replacer.proto. It is referred from other resource protos.

  1syntax = "proto3";
  2package kernel;
  3
  4import "buf/validate/validate.proto";
  5import "kernel/encoding.proto";
  6import "kernel/hash.proto";
  7import "kernel/commonkey.proto";
  8
  9option go_package = "github.com/aileron-gateway/aileron-gateway/apis/kernel";
 10
 11message ReplacerSpec {
 12    oneof Replacers {
 13        // Fixed is the fixed value replacer.
 14        // This replacer replaces input to a fixed value configured.
 15        FixedReplacer Fixed = 1 [json_name = "fixed"];
 16        // Value is the configured values replacer.
 17        // This replacer replaces a configured value to another.
 18        ValueReplacer Value = 2 [json_name = "value"];
 19        // Left is the left side replacer.
 20        // This replacer replaces left side of a string
 21        // with the configured characters.
 22        LeftReplacer Left = 3 [json_name = "left"];
 23        // Right is the right side replacer.
 24        // This replacer replaces right side of a string
 25        // with the configured characters.
 26        RightReplacer Right = 4 [json_name = "right"];
 27        // Trim is the replacer that trims characters from
 28        // both side of a string.
 29        // This leverages https://pkg.go.dev/strings#Trim
 30        TrimReplacer Trim = 5 [json_name = "trim"];
 31        // TrimLeft is the replacer that trims characters from
 32        // left side of a string.
 33        // This leverages https://pkg.go.dev/strings#TrimLeft
 34        TrimLeftReplacer TrimLeft = 6 [json_name = "trimLeft"];
 35        // TrimRight is the replacer that trims characters from
 36        // right side of a string.
 37        // This leverages https://pkg.go.dev/strings#TrimRight
 38        TrimRightReplacer TrimRight = 7 [json_name = "trimRight"];
 39        // TrimPrefix is the replacer that replace a prefix if matched.
 40        // This leverages https://pkg.go.dev/strings#TrimPrefix
 41        TrimPrefixReplacer TrimPrefix = 8 [json_name = "trimPrefix"];
 42        // TrimSuffix is the replacer that replace a suffix if matched.
 43        // This leverages https://pkg.go.dev/strings#TrimSuffix
 44        TrimSuffixReplacer TrimSuffix = 9 [json_name = "trimSuffix"];
 45        // Encode is the replacer that replace string
 46        // to encoded values.
 47        EncodeReplacer Encode = 10 [json_name = "encode"];
 48        // Hash is the replacer that replace string
 49        // to hashed values.
 50        HashReplacer Hash = 11 [json_name = "hash"];
 51        // Regexp is the replacer that replace string
 52        // using regular expression.
 53        // See https://pkg.go.dev/regexp#Regexp.ReplaceAllString
 54        RegexpReplacer Regexp = 12 [json_name = "regexp"];
 55        // Expand is the replacer that replace string
 56        // using regular expression.
 57        // See https://pkg.go.dev/regexp#Regexp.ExpandString
 58        ExpandReplacer Expand = 13 [json_name = "expand"];
 59        // Encrypt is the replacer that replace string
 60        // to encrypted values.
 61        EncryptReplacer Encrypt = 14 [json_name = "encrypt"];
 62        // HMAC is the replacer that replace string
 63        // to hmac hash values.
 64        HMACReplacer HMAC = 15 [json_name = "hmac"];
 65    }
 66}
 67
 68// ReplaceType is the replacing methods of string or bytes data.
 69enum ReplaceType {
 70    Fixed      = 0;
 71    Value      = 1;
 72    Left       = 2;
 73    Right      = 3;
 74    Trim       = 4;
 75    TrimLeft   = 5;
 76    TrimRight  = 6;
 77    TrimPrefix = 7;
 78    TrimSuffix = 8;
 79    Encode     = 9;
 80    Hash       = 10;
 81    Regexp     = 11;
 82    Expand     = 12;
 83    Encrypt    = 13;
 84    HMAC       = 14;
 85}
 86
 87message FixedReplacer {
 88    // [OPTIONAL]
 89    string Value = 1 [json_name = "value"];
 90}
 91
 92message ValueReplacer {
 93    // [OPTIONAL]
 94    // FromTo is the pairs of 2 valus.
 95    // Key is the string which is replaced by the value.
 96    // Value is the string which replaces the key.
 97    // Default is not set.
 98    map<string, string> FromTo = 1 [json_name = "fromTo"];
 99}
100
101message LeftReplacer {
102    // [OPTIONAL]
103    // Char is the character to replaces.
104    string Char = 1 [json_name = "char"];
105    // [OPTIONAL]
106    // Length is the character length that is
107    // replaced from the left side of a string.
108    // Default is not set, or 0.
109    uint32 Length = 2 [json_name = "length"];
110}
111
112message RightReplacer {
113    // [OPTIONAL]
114    // Char is the character to replaces.
115    string Char = 1 [json_name = "char"];
116    // [OPTIONAL]
117    // Length is the character length that is
118    // replaced from the right side of a string.
119    // Default is not set, or 0.
120    uint32 Length = 2 [json_name = "length"];
121}
122
123message TrimReplacer {
124    // [OPTIONAL]
125    // CutSets is the sets of character to be trimmed
126    // from both side of strings.
127    // This uses https://pkg.go.dev/strings#Trim
128    repeated string CutSets = 1 [json_name = "cutSets"];
129}
130
131message TrimLeftReplacer {
132    // [OPTIONAL]
133    // CutSets is the sets of character to be trimmed
134    // from left side of strings.
135    // This uses https://pkg.go.dev/strings#TrimLeft
136    repeated string CutSets = 1 [json_name = "cutSets"];
137}
138
139message TrimRightReplacer {
140    // [OPTIONAL]
141    // CutSets is the sets of character to be trimmed
142    // from right side of strings.
143    // This uses https://pkg.go.dev/strings#TrimRight
144    repeated string CutSets = 1 [json_name = "cutSets"];
145}
146
147message TrimPrefixReplacer {
148    // [OPTIONAL]
149    // Prefixes is the strings that are removed
150    // from the target strings.
151    // This uses https://pkg.go.dev/strings#TrimPrefix
152    repeated string Prefixes = 1 [json_name = "prefixes"];
153}
154
155message TrimSuffixReplacer {
156    // [OPTIONAL]
157    // Suffixes is the strings that are removed
158    // from the target strings.
159    // This uses https://pkg.go.dev/strings#TrimSuffix
160    repeated string Suffixes = 1 [json_name = "suffixes"];
161}
162
163message EncodeReplacer {
164    // [OPTIONAL]
165    // Pattern is the strings pattern that should be replaced.
166    // If not set or empty, the entire value is considered to be matched.
167    // Default is not set.
168    string Pattern = 1 [json_name = "pattern"];
169    // [OPTIONAL]
170    // POSIX is the flag to use POSIX regular expression.
171    // See https://pkg.go.dev/regexp#CompilePOSIX
172    // Default is false.
173    bool POSIX = 2 [json_name = "posix"];
174    // [REQUIRED]
175    // Encoding is the types of encoding to encode the matched values.
176    EncodingType Encoding = 3 [json_name = "encoding", (buf.validate.field).required = true];
177}
178
179message HashReplacer {
180    // [OPTIONAL]
181    // Pattern is the strings pattern that should be replaced.
182    // If not set or empty, the entire value is considered to be matched.
183    // Default is not set.
184    string Pattern = 1 [json_name = "pattern"];
185    // [OPTIONAL]
186    // POSIX is the flag to use POSIX regular expression.
187    // See https://pkg.go.dev/regexp#CompilePOSIX
188    // Default is false.
189    bool POSIX = 2 [json_name = "posix"];
190    // [REQUIRED]
191    // Alg is the hash algorithm to hash the matched values.
192    HashAlg Alg = 3 [json_name = "alg", (buf.validate.field).required = true];
193    // [REQUIRED]
194    // Encoding is the types of encoding to encode the hash values.
195    EncodingType Encoding = 4 [json_name = "encoding", (buf.validate.field).required = true];
196}
197
198message RegexpReplacer {
199    // [REQUIRED]
200    // Pattern is the strings pattern that should be replaced.
201    string Pattern = 1 [json_name = "pattern", (buf.validate.field).string.min_len = 1];
202    // [OPTIONAL]
203    // Replace is the replace pattern.
204    // Default is not set.
205    string Replace = 2 [json_name = "replace"];
206    // [OPTIONAL]
207    // POSIX is the flag to use POSIX regular expression.
208    // See https://pkg.go.dev/regexp#CompilePOSIX
209    bool POSIX = 3 [json_name = "posix"];
210    // [OPTIONAL]
211    // Literal is the flag to use the replace value as literal value.
212    // See https://pkg.go.dev/regexp#Regexp.ReplaceAllLiteral
213    bool Literal = 4 [json_name = "literal"];
214}
215
216message ExpandReplacer {
217    // [REQUIRED]
218    // Pattern is the strings pattern that should be replaced.
219    string Pattern = 1 [json_name = "pattern", (buf.validate.field).string.min_len = 1];
220    // [OPTIONAL]
221    // Replace is the replace pattern.
222    // Default is not set.
223    string Template = 2 [json_name = "template"];
224    // [OPTIONAL]
225    // POSIX is the flag to use POSIX regular expression.
226    // See https://pkg.go.dev/regexp#CompilePOSIX
227    bool POSIX = 3 [json_name = "posix"];
228}
229
230message EncryptReplacer {
231    // [OPTIONAL]
232    // Pattern is the strings pattern that should be replaced.
233    // If not set or empty, the entire value is considered to be matched.
234    // Default is not set.
235    string Pattern = 1 [json_name = "pattern"];
236    // [OPTIONAL]
237    // POSIX is the flag to use POSIX regular expression.
238    // See https://pkg.go.dev/regexp#CompilePOSIX
239    // Default is false.
240    bool POSIX = 2 [json_name = "posix"];
241    // [REQUIRED]
242    // Alg is the common key encryption algorithm.
243    CommonKeyCryptType Alg = 3 [json_name = "alg", (buf.validate.field).required = true];
244    // [REQUIRED]
245    // Encoding is the types of encoding to encode the hash values.
246    EncodingType Encoding = 4 [json_name = "encoding", (buf.validate.field).required = true];
247    // [REQUIRED]
248    // Password is the hex encoded password string
249    // for the common key encryption.
250    // Password length must be the valid length corresponding to the algorithm.
251    // 16,24,32 bytes for AES, 8 bytes for DES, 24 bytes for 3DES, 1-256 bytes for RC4.
252    // Default is not set.
253    string Password = 5 [json_name = "password", (buf.validate.field).required = true];
254}
255
256message HMACReplacer {
257    // [OPTIONAL]
258    // Pattern is the strings pattern that should be replaced.
259    // If not set or empty, the entire value is considered to be matched.
260    // Default is not set.
261    string Pattern = 1 [json_name = "pattern"];
262    // [OPTIONAL]
263    // POSIX is the flag to use POSIX regular expression.
264    // See https://pkg.go.dev/regexp#CompilePOSIX
265    // Default is false.
266    bool POSIX = 2 [json_name = "posix"];
267    // [REQUIRED]
268    // Alg is the hash algorithm to hash the matched values.
269    HashAlg Alg = 3 [json_name = "alg", (buf.validate.field).required = true];
270    // [REQUIRED]
271    // Encoding is the types of encoding to encode the hash values.
272    EncodingType Encoding = 4 [json_name = "encoding", (buf.validate.field).required = true];
273    // [REQUIRED]
274    // Key is the hex encoded key string for HMAC.
275    // Default is not set.
276    string Key = 5 [json_name = "key", (buf.validate.field).required = true];
277}

YAML Structure

# Oneof
fixed: <object> # FixedReplacer
value: <object> # ValueReplacer
left: <object> # LeftReplacer
right: <object> # RightReplacer
trim: <object> # TrimReplacer
trimLeft: <object> # TrimLeftReplacer
trimRight: <object> # TrimRightReplacer
trimPrefix: <object> # TrimPrefixReplacer
trimSuffix: <object> # TrimSuffixReplacer
encode: <object> # EncodeReplacer
hash: <object> # HashReplacer
regexp: <object> # RegexpReplacer
expand: <object> # ExpandReplacer
encrypt: <object> # EncryptReplacer
hmac: <object> # HMACReplacer

Examples


Last modified June 7, 2025: add japanese (f2a41f1)