-
Notifications
You must be signed in to change notification settings - Fork 23
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
fix(tokens-info): Refactor delete missing keys for keep internal keys #285
Open
dievazqu
wants to merge
5
commits into
main
Choose a base branch
from
fix-delete-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4004c3a
Refactor delete missing keys for keep internal keys
dievazqu da728b5
Improving naming
dievazqu 317cbf4
Merge branch 'main' into fix-delete-keys
dievazqu 9a090ab
Fixing diff + simplifying the reduce logic
dievazqu 5eb4532
Fix lint
dievazqu 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import Joi from 'joi' | ||
|
||
export class OverrideType { | ||
static readonly OverrideAll = new OverrideType(true, true) | ||
static readonly OnlyUpdates = new OverrideType(false, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't used. |
||
static readonly DeleteMissingKeysAndUpdate = new OverrideType(false, true) | ||
static readonly OverrideAll = new OverrideType(true, []) | ||
static readonly KeepInternalKeys = (keepInternalKeys?: string[]) => | ||
new OverrideType(false, keepInternalKeys) | ||
|
||
// private to disallow creating other instances of this type | ||
private constructor( | ||
public readonly shouldOverride: boolean, | ||
public readonly deleteMissingKeys: boolean, | ||
public readonly keptInternalKeys: string[] = [], | ||
) {} | ||
} | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,128 +1,91 @@ | ||
import { deleteMissingKeysUpdateRequest, mapNestedJsonIntoPlain } from './utils' | ||
import { keepInternalKeys } from './utils' | ||
|
||
describe('Keep Internal Keys', () => { | ||
const valuesToUpdate = { | ||
'0x1234': { | ||
address: '0x1234', | ||
decimal: 18, | ||
imageUrl: 'https://example.com', | ||
}, | ||
'0x1235': { | ||
address: '0x1235', | ||
decimal: 8, | ||
imageUrl: 'https://example2.com', | ||
}, | ||
'0x1236': { | ||
address: '0x1236', | ||
decimal: 18, | ||
imageUrl: 'https://example3.com', | ||
}, | ||
} | ||
|
||
const currentValues = { | ||
'0x1234': { | ||
address: '0x1234old', | ||
decimal: 18, | ||
imageUrl: 'https://example.com', | ||
usdPrice: '1', | ||
historicalUsdPrices: { | ||
lastDay: { | ||
price: '1.2', | ||
}, | ||
}, | ||
}, | ||
'0x1235': { | ||
address: '0x1235old', | ||
decimal: 8, | ||
imageUrl: 'https://example2.com', | ||
usdPrice: '2', | ||
historicalUsdPrices: { | ||
lastDay: { | ||
price: '2.2', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
describe('Map Nested Json Into', () => { | ||
it('should map correctly when the json contains one level', () => { | ||
const inputJson = { | ||
key1: 'stringValue', | ||
key2: 3, | ||
} | ||
const expectedJson = inputJson | ||
it("returns the expected object when there aren't keys to keep", () => { | ||
const updateObject = keepInternalKeys(valuesToUpdate, currentValues, []) | ||
|
||
const result = mapNestedJsonIntoPlain(inputJson) | ||
expect(result).toMatchObject(expectedJson) | ||
expect(updateObject).toMatchObject(valuesToUpdate) | ||
}) | ||
|
||
it('should map correctly when the json contains a nested level', () => { | ||
const inputJson = { | ||
key1: { | ||
key3: 4, | ||
key4: 'stringValue', | ||
it('returns the expected object when there are internal keys to keep', () => { | ||
const updateObject = keepInternalKeys(valuesToUpdate, currentValues, [ | ||
'usdPrice', | ||
'historicalUsdPrices', | ||
]) | ||
|
||
const expectedObject = { | ||
'0x1234': { | ||
address: '0x1234', | ||
decimal: 18, | ||
imageUrl: 'https://example.com', | ||
usdPrice: '1', | ||
historicalUsdPrices: { | ||
lastDay: { | ||
price: '1.2', | ||
}, | ||
}, | ||
}, | ||
key2: 3, | ||
} | ||
const expectedJson = { | ||
'key1/key3': 4, | ||
'key1/key4': 'stringValue', | ||
key2: 3, | ||
} | ||
|
||
const result = mapNestedJsonIntoPlain(inputJson) | ||
expect(result).toMatchObject(expectedJson) | ||
}) | ||
|
||
it('should map correctly when the json contains multiple nested level', () => { | ||
const inputJson = { | ||
key1: { | ||
key3: 4, | ||
key4: 5, | ||
key5: { | ||
key6: 'stringValue', | ||
key7: { | ||
key8: 9, | ||
'0x1235': { | ||
address: '0x1235', | ||
decimal: 8, | ||
imageUrl: 'https://example2.com', | ||
usdPrice: '2', | ||
historicalUsdPrices: { | ||
lastDay: { | ||
price: '2.2', | ||
}, | ||
}, | ||
}, | ||
key2: 3, | ||
} | ||
const expectedJson = { | ||
'key1/key3': 4, | ||
'key1/key4': 5, | ||
'key1/key5/key6': 'stringValue', | ||
'key1/key5/key7/key8': 9, | ||
key2: 3, | ||
} | ||
|
||
const result = mapNestedJsonIntoPlain(inputJson) | ||
expect(result).toMatchObject(expectedJson) | ||
}) | ||
|
||
it('should map array to object with indexes as key', () => { | ||
const inputJson = { | ||
key1: ['this', 'is', 'a', 'test'], | ||
key2: 3, | ||
} | ||
|
||
const expectedJson = { | ||
'key1/0': 'this', | ||
'key1/1': 'is', | ||
'key1/2': 'a', | ||
'key1/3': 'test', | ||
key2: 3, | ||
} | ||
|
||
const result = mapNestedJsonIntoPlain(inputJson) | ||
expect(result).toMatchObject(expectedJson) | ||
}) | ||
}) | ||
|
||
describe('Delete Missing Keys UpdateRequest', () => { | ||
it('return expected update request when there are missing keys', () => { | ||
const expected = { | ||
key1: 1, | ||
key2: 'value', | ||
} | ||
|
||
const current = { | ||
key1: 2, | ||
key3: 'shouldBeDeleted', | ||
} | ||
|
||
const updateObject = deleteMissingKeysUpdateRequest(expected, current) | ||
|
||
const expectedResult = { | ||
key3: null, | ||
} | ||
|
||
expect(updateObject).toMatchObject(expectedResult) | ||
}) | ||
|
||
it('return empty update request when there are not missing keys', () => { | ||
const expected = { | ||
key1: 1, | ||
key2: 'value', | ||
key3: 'extra', | ||
} | ||
|
||
const current = { | ||
key1: 2, | ||
key2: 'shouldNotBeDeleted', | ||
} | ||
|
||
const updateObject = deleteMissingKeysUpdateRequest(expected, current) | ||
|
||
expect(updateObject).toMatchObject({}) | ||
}) | ||
|
||
it('creates all keys if the RTDB instance is clean', () => { | ||
const expected = { | ||
key1: 1, | ||
key2: 'value', | ||
'0x1236': { | ||
address: '0x1236', | ||
decimal: 18, | ||
imageUrl: 'https://example3.com', | ||
}, | ||
} | ||
|
||
const current = null | ||
|
||
const updateObject = deleteMissingKeysUpdateRequest(expected, current) | ||
|
||
expect(updateObject).toMatchObject({}) | ||
expect(expectedObject).toMatchObject(updateObject) | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,41 +1,19 @@ | ||
export function mapNestedJsonIntoPlain(json: any): any { | ||
if (!isNonEmptyObject(json)) { | ||
return json | ||
} | ||
export function keepInternalKeys( | ||
expected: any, | ||
current: any, | ||
keptInternalKeys: string[], | ||
) { | ||
const result: any = {} | ||
|
||
return Object.keys(json).reduce( | ||
(result, current) => ({ | ||
...result, | ||
...prependKey(current, mapNestedJsonIntoPlain(json[current])), | ||
}), | ||
{}, | ||
) | ||
} | ||
for (const key of Object.keys(expected)) { | ||
result[key] = expected[key] | ||
|
||
function prependKey(key: string, json: any) { | ||
if (!isNonEmptyObject(json)) { | ||
return { [key]: json } | ||
for (const internalKey of keptInternalKeys) { | ||
if (current[key] && current[key][internalKey]) { | ||
result[key][internalKey] = current[key][internalKey] | ||
} | ||
} | ||
} | ||
|
||
return Object.keys(json).reduce( | ||
(result, current) => ({ | ||
...result, | ||
[`${key}/${current}`]: json[current], | ||
}), | ||
{}, | ||
) | ||
} | ||
|
||
function isNonEmptyObject(json: any): boolean { | ||
return typeof json === 'object' && json !== null | ||
} | ||
|
||
export function deleteMissingKeysUpdateRequest(expected: any, current: any) { | ||
if (!isNonEmptyObject(current)) return {} | ||
const expectedKeys = new Set(Object.keys(expected)) | ||
const currentKeys = Object.keys(current) | ||
|
||
return currentKeys | ||
.filter((key) => !expectedKeys.has(key)) | ||
.reduce((result, key) => ({ ...result, [key]: null }), {}) | ||
return result | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this might a bit misleading, but this was only removing missing keys in the root level, so basically it was removing the whole token data if not present.