-
Notifications
You must be signed in to change notification settings - Fork 171
✨[RUM-10962][Remote config] support js strategy #3766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6bdcf5c
♻️ factorize extractor
bcaudan 9e013d2
✨add simple JSON path parser
bcaudan 06fe14f
✨add js strategy support
bcaudan 3d387f1
Merge branch 'main' into bcaudan/remote-config6
bcaudan 0a508d0
⬆️ synchronize remote configuration schema
bcaudan 8d4adc5
👌remove unneeded cast
bcaudan ad3b79e
👌add extra comments
bcaudan dfa515d
👌replace sets by lists
bcaudan 8fa318d
👌use String.raw to avoid quote juggling
bcaudan 81b8175
👌some renamings
bcaudan 0d2b242
👌remove useless types
bcaudan fd6b558
👌rework namings
bcaudan 081ac90
🐛fix escaping logic
bcaudan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/rum-core/src/domain/configuration/jsonPathParser.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { parseJsonPath } from './jsonPathParser' | ||
|
|
||
| describe('parseJsonPath', () => { | ||
| it('should parse variable names with dot notation', () => { | ||
| expect(parseJsonPath('a')).toEqual(['a']) | ||
| expect(parseJsonPath('foo.bar')).toEqual(['foo', 'bar']) | ||
| expect(parseJsonPath('foo.bar.qux')).toEqual(['foo', 'bar', 'qux']) | ||
| }) | ||
|
|
||
| it('should parse property names with bracket notation', () => { | ||
| expect(parseJsonPath("['a']")).toEqual(['a']) | ||
| expect(parseJsonPath('["a"]')).toEqual(['a']) | ||
| expect(parseJsonPath('[\'foo\']["bar"]')).toEqual(['foo', 'bar']) | ||
| expect(parseJsonPath("['foo']['bar']['qux']")).toEqual(['foo', 'bar', 'qux']) | ||
| }) | ||
|
|
||
| it('should parse variable and property names mixed', () => { | ||
| expect(parseJsonPath("['foo'].bar['qux']")).toEqual(['foo', 'bar', 'qux']) | ||
| }) | ||
|
|
||
| it('should parse array indexes', () => { | ||
| expect(parseJsonPath('[0]')).toEqual(['0']) | ||
| expect(parseJsonPath('foo[12]')).toEqual(['foo', '12']) | ||
| expect(parseJsonPath("['foo'][12]")).toEqual(['foo', '12']) | ||
| }) | ||
|
|
||
| it('should parse property names with unsupported variable name characters', () => { | ||
| expect(parseJsonPath("['foo\\n']")).toEqual(['foo\\n']) | ||
| expect(parseJsonPath("['foo\\'']")).toEqual(["foo\\'"]) | ||
| expect(parseJsonPath('["foo\\""]')).toEqual(['foo\\"']) | ||
| expect(parseJsonPath("['foo[]']")).toEqual(['foo[]']) | ||
| }) | ||
|
|
||
| it('should return an empty array for an invalid path', () => { | ||
| expect(parseJsonPath('.foo')).toEqual([]) | ||
| expect(parseJsonPath('.')).toEqual([]) | ||
| expect(parseJsonPath('foo.')).toEqual([]) | ||
| expect(parseJsonPath('foo..bar')).toEqual([]) | ||
| expect(parseJsonPath("[['foo']")).toEqual([]) | ||
| expect(parseJsonPath("['foo'")).toEqual([]) | ||
| expect(parseJsonPath("['foo]")).toEqual([]) | ||
| expect(parseJsonPath('[1')).toEqual([]) | ||
| expect(parseJsonPath('foo]')).toEqual([]) | ||
| expect(parseJsonPath("[foo']")).toEqual([]) | ||
| expect(parseJsonPath("['foo''bar']")).toEqual([]) | ||
| expect(parseJsonPath("['foo\\o']")).toEqual([]) | ||
| expect(parseJsonPath("['foo']a")).toEqual([]) | ||
| expect(parseJsonPath('["foo\']')).toEqual([]) | ||
| }) | ||
| }) | ||
148 changes: 148 additions & 0 deletions
148
packages/rum-core/src/domain/configuration/jsonPathParser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /** | ||
| * Extract path parts from a simple JSON path expression, return [] for an invalid path | ||
| * | ||
| * Supports: | ||
| * - Dot notation: `foo.bar.baz` | ||
| * - Bracket notation: `['foo']["bar"]` | ||
| * - Array indices: `items[0]`, `data['users'][1]` | ||
| * | ||
| * Examples: | ||
| * parseJsonPath("['foo'].bar[12]") | ||
| * => ['foo', 'bar', '12'] | ||
| * | ||
| * parseJsonPath("['foo") | ||
| * => [] | ||
| * | ||
| * | ||
| * Useful references: | ||
| * - https://goessner.net/articles/JsonPath/ | ||
| * - https://jsonpath.com/ | ||
| * - https://github.com/jsonpath-standard | ||
| */ | ||
| export function parseJsonPath(path: string): string[] { | ||
bcaudan marked this conversation as resolved.
Show resolved
Hide resolved
amortemousque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const pathParts: string[] = [] | ||
| let previousToken = Token.START | ||
| let currentToken: Token | undefined | ||
| let quoteContext: string | undefined | ||
| let currentPathPart = '' | ||
| for (const char of path) { | ||
| // find which kind of token is this char | ||
| currentToken = findInSet(ALLOWED_NEXT_TOKENS[previousToken], (token) => TOKEN_PREDICATE[token](char, quoteContext)) | ||
| if (!currentToken) { | ||
| return [] | ||
| } | ||
| if (ALLOWED_PATH_PART_TOKENS.has(currentToken)) { | ||
| // buffer the char if it belongs to the path part | ||
| // ex: foo['bar'] | ||
| // ^ ^ | ||
| currentPathPart += char | ||
| } else if (ALLOWED_PATH_PART_DELIMITER_TOKENS.has(currentToken) && currentPathPart !== '') { | ||
| // close the current path part if we have reach a path part delimiter | ||
| // ex: foo.bar['qux'] | ||
| // ^ ^ ^ | ||
| pathParts.push(currentPathPart) | ||
| currentPathPart = '' | ||
| } else if (currentToken === Token.QUOTE_START) { | ||
| quoteContext = char | ||
| } else if (currentToken === Token.QUOTE_END) { | ||
| quoteContext = undefined | ||
| } | ||
| previousToken = currentToken | ||
| } | ||
| if (!ALLOWED_NEXT_TOKENS[previousToken].has(Token.END)) { | ||
| return [] | ||
| } | ||
| if (currentPathPart !== '') { | ||
| pathParts.push(currentPathPart) | ||
| } | ||
| return pathParts | ||
| } | ||
|
|
||
| const enum Token { | ||
| START, | ||
| END, | ||
|
|
||
| VARIABLE_FIRST_LETTER, | ||
| VARIABLE_LETTER, | ||
| DOT, | ||
|
|
||
| BRACKET_START, | ||
| BRACKET_END, | ||
| NUMBER_LETTER, | ||
|
|
||
| QUOTE_START, | ||
| QUOTE_END, | ||
| QUOTE_PROPERTY_LETTER, | ||
| QUOTE_ESCAPE, | ||
| QUOTE_ESCAPABLE_LETTER, | ||
| } | ||
bcaudan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const VARIABLE_FIRST_LETTER = /[a-zA-Z_$]/ | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const VARIABLE_LETTER = /[a-zA-Z0-9_$]/ | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const NUMBER_CHAR = /[0-9]/ | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const QUOTE_ESCAPABLE_LETTERS = '/\\bfnrtu' | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const QUOTE_CHAR = '\'"' | ||
|
|
||
| const TOKEN_PREDICATE: { [token in Token]: (char: string, quoteContext?: string) => boolean } = { | ||
| // no char should match to START or END | ||
| [Token.START]: (_: string) => false, | ||
| [Token.END]: (_: string) => false, | ||
|
|
||
| [Token.VARIABLE_FIRST_LETTER]: (char: string) => VARIABLE_FIRST_LETTER.test(char), | ||
| [Token.VARIABLE_LETTER]: (char: string) => VARIABLE_LETTER.test(char), | ||
| [Token.DOT]: (char: string) => char === '.', | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [Token.BRACKET_START]: (char: string) => char === '[', | ||
| [Token.BRACKET_END]: (char: string) => char === ']', | ||
| [Token.NUMBER_LETTER]: (char: string) => NUMBER_CHAR.test(char), | ||
|
|
||
| [Token.QUOTE_START]: (char: string) => QUOTE_CHAR.includes(char), | ||
| [Token.QUOTE_END]: (char: string, quoteContext?: string) => char === quoteContext, | ||
| [Token.QUOTE_PROPERTY_LETTER]: (_: string) => true, // any char can be used in property | ||
| [Token.QUOTE_ESCAPE]: (char: string) => char === '\\', | ||
| [Token.QUOTE_ESCAPABLE_LETTER]: (char: string, quoteContext?: string) => | ||
| `${quoteContext}${QUOTE_ESCAPABLE_LETTERS}`.includes(char), | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const ALLOWED_NEXT_TOKENS: { [token in Token]: Set<Token> } = { | ||
| [Token.START]: new Set([Token.VARIABLE_FIRST_LETTER, Token.BRACKET_START]), | ||
| [Token.END]: new Set([]), | ||
|
|
||
| [Token.VARIABLE_FIRST_LETTER]: new Set([Token.VARIABLE_LETTER, Token.DOT, Token.BRACKET_START, Token.END]), | ||
| [Token.VARIABLE_LETTER]: new Set([Token.VARIABLE_LETTER, Token.DOT, Token.BRACKET_START, Token.END]), | ||
| [Token.DOT]: new Set([Token.VARIABLE_FIRST_LETTER]), | ||
|
|
||
| [Token.BRACKET_START]: new Set([Token.QUOTE_START, Token.NUMBER_LETTER]), | ||
| [Token.BRACKET_END]: new Set([Token.DOT, Token.BRACKET_START, Token.END]), | ||
| [Token.NUMBER_LETTER]: new Set([Token.NUMBER_LETTER, Token.BRACKET_END]), | ||
|
|
||
| [Token.QUOTE_START]: new Set([Token.QUOTE_ESCAPE, Token.QUOTE_END, Token.QUOTE_PROPERTY_LETTER]), | ||
| [Token.QUOTE_END]: new Set([Token.BRACKET_END]), | ||
| [Token.QUOTE_PROPERTY_LETTER]: new Set([Token.QUOTE_ESCAPE, Token.QUOTE_END, Token.QUOTE_PROPERTY_LETTER]), | ||
| [Token.QUOTE_ESCAPE]: new Set([Token.QUOTE_ESCAPABLE_LETTER]), | ||
| [Token.QUOTE_ESCAPABLE_LETTER]: new Set([Token.QUOTE_ESCAPE, Token.QUOTE_END, Token.QUOTE_PROPERTY_LETTER]), | ||
| } | ||
bcaudan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // foo['bar\n'][12] | ||
| // ^^ ^ ^^ ^ | ||
| const ALLOWED_PATH_PART_TOKENS = new Set([ | ||
| Token.VARIABLE_FIRST_LETTER, | ||
| Token.VARIABLE_LETTER, | ||
| Token.NUMBER_LETTER, | ||
|
|
||
| Token.QUOTE_PROPERTY_LETTER, | ||
| Token.QUOTE_ESCAPE, | ||
| Token.QUOTE_ESCAPABLE_LETTER, | ||
| ]) | ||
|
|
||
| // foo.bar['qux'] | ||
| // ^ ^ ^ | ||
| const ALLOWED_PATH_PART_DELIMITER_TOKENS = new Set([Token.DOT, Token.BRACKET_START, Token.BRACKET_END]) | ||
|
|
||
| function findInSet<T>(set: Set<T>, predicate: (item: T) => boolean): T | undefined { | ||
| for (const item of set) { | ||
| if (predicate(item)) { | ||
| return item | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.