-
Notifications
You must be signed in to change notification settings - Fork 75
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: Resolve Type Warnings for ConfigService.get() #3350
base: main
Are you sure you want to change the base?
Fix: Resolve Type Warnings for ConfigService.get() #3350
Conversation
- Ensured type consistency in all affected test cases. - Removed unnecessary type casting or added necessary casting. Signed-off-by: belloibrahv <[email protected]>
- Ensured type consistency in all affected test cases. - Removed unnecessary type casting or added necessary casting. Signed-off-by: belloibrahv <[email protected]>
@Nana-EC @ebadiere @quiet-node I've submitted a PR addressing issue #3142 to resolve type warnings for ConfigService.get(). It ensures type consistency across test cases by refining type usage and adding/removing the necessary casting. Let me know if further changes are needed. Thank you for reviewing! |
@@ -26,7 +28,7 @@ export interface ConfigProperty { | |||
} | |||
|
|||
export class GlobalConfig { | |||
public static readonly ENTRIES: Record<string, ConfigProperty> = { | |||
public static readonly ENTRIES: Record<ConfigName, ConfigProperty> = { |
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.
Instead of enumerating the keys manually, can we do something like
const _CONFIG = {
BATCH_REQUESTS_ENABLED: {
envName: 'BATCH_REQUESTS_ENABLED',
type: 'boolean',
required: false,
defaultValue: null,
},
//...
WS_SUBSCRIPTION_LIMIT: {
envName: 'WS_SUBSCRIPTION_LIMIT',
type: 'number',
required: false,
defaultValue: null,
},
} satisfies { [key: string]: ConfigProperty };
export type ConfigKey = keyof typeof _CONFIG;
export class GlobalConfig {
public static readonly ENTRIES: Record<ConfigKey, ConfigProperty> = _CONFIG;
}
?
So we retain type safety for the keys (even if they're strings).
Note that we can use satisfies
because we're using TypeScript 4.9.5
as defined in package-lock.json
.
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.
Ohh I like the suggestion! Looks cleaner for sure 👏🏼
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.
Thanks for the contribution.
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC |
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.
nit: revert
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC |
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.
revert this and any other license header changes where you're changing an old header date into this year.
Might be a bug in an automated script
|
||
describe('@server-config Server Configuration Options Coverage', function () { | ||
describe('Koa Server Timeout', () => { | ||
it('should timeout a request after the specified time', async () => { | ||
const requestTimeoutMs: number = parseInt(ConfigService.get('SERVER_REQUEST_TIMEOUT_MS') || '3000'); | ||
const requestTimeoutMs: number = parseInt(ConfigService.get(ConfigName.SERVER_REQUEST_TIMEOUT_MS) as string || '3000'); |
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.
I wonder if we should add a getString and do the casting in the ConfigService to make this easier on devs.
Thinking out loud and getting thoughts.
No need to change in this PR
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.
Thank you for the suggestion about ConfigService.getString
. I agree that it could streamline type handling. I suggest we open a separate issue to implement and test this change comprehensively.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3350 +/- ##
===========================================
- Coverage 85.04% 49.70% -35.35%
===========================================
Files 65 66 +1
Lines 4508 4601 +93
Branches 1023 1023
===========================================
- Hits 3834 2287 -1547
- Misses 330 1926 +1596
- Partials 344 388 +44
Flags with carried forward coverage won't be shown. Click here to find out more.
|
27253f5
to
7f440e4
Compare
@Nana-EC @acuarica @quiet-node, Could you please review my updates and let me know if further adjustments are needed? |
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.
Hi @belloibrahv, thanks for taking the time to send this, it's looking good. Left a couple of suggestions we can continue iterating on to improve the config.
@@ -49,13 +50,13 @@ describe('ConfigService tests', async function () { | |||
}); | |||
|
|||
it('should be able to get existing env var', async () => { | |||
const res = ConfigService.get('CHAIN_ID'); | |||
const res = ConfigService.get('CHAIN_ID' as ConfigKey); |
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.
Can we change the definition of ConfigService.get
to get(name: ConfigKey)
? So we can then avoid all these type assertions, i.e., as ConfigKey
.
Not related with your PR, but I tried this change locally and I saw there was a configuration entry missing, SERVER_HOST
. If we change the definition of get
as mentioned above, we might want add a SERVER_HOST
entry.
packages/config-service/tests/src/services/configService.spec.ts
Outdated
Show resolved
Hide resolved
Also @belloibrahv please take a look at #3350 (comment). |
Signed-off-by: belloibrahv <[email protected]>
Signed-off-by: belloibrahv <[email protected]>
Signed-off-by: belloibrahv <[email protected]>
7f440e4
to
c7e6a08
Compare
@quiet-node @acuarica @Nana-EC @shemnon , thank you for the suggestion. I've implemented the following changes based on your feedback:
Let me know if there's anything else you'd like me to address! |
@acuarica, Thank you for the suggestion! I think adding a Would you suggest we implement these methods for this task or address it as a follow-up? Also, should these methods throw errors for mismatched types, or return defaults like |
Let's do it in a separate PR after this. Also appreciate the patience. |
Hi @quiet-node, @acuarica, @Nana-EC, @shemnon, Hope you all had a wonderful holiday! When you have a moment, please review and provide feedback on the requested changes to the PR. Thank you for your understanding! |
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.
Hi @belloibrahv, hope you had a wonderful holiday too! It's looking good, left some comments.
We appreciate your patience on this. Next week the full team will be back from holidays, so you'll get more reviews.
packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts
Outdated
Show resolved
Hide resolved
Another change we could implement to improve type safety, could be to avoid casting the return type of
We need some type definitions type TypeStrToType<Tstr extends string> = Tstr extends 'string'
? string
: Tstr extends 'boolean'
? boolean
: Tstr extends 'number'
? number
: Tstr extends 'array'
? unknown[] // could be also `any[]`
: never;
type GetTypeStrOfKey<K extends string> = K extends keyof typeof _CONFIG ? typeof _CONFIG[K]['type'] : never;
export type TypeOfKey<K extends string> = TypeStrToType<GetTypeStrOfKey<K>>; These type definitions would allows to get right type based on the config key. For example Then, we can update our public static get<K extends ConfigKey>(name: K): TypeOfKey<K> | undefined So now the correct return type is inferred based on the config key |
Thanks @belloibrahv , hope you had a good one too. |
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.
Great work! Please revert the unnecessary changes and additions. I’ve added a suggestion on the ConfigService.get() method.
@@ -26,7 +28,7 @@ export interface ConfigProperty { | |||
} | |||
|
|||
export class GlobalConfig { | |||
public static readonly ENTRIES: Record<string, ConfigProperty> = { | |||
public static readonly ENTRIES: Record<ConfigName, ConfigProperty> = { |
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.
Ohh I like the suggestion! Looks cleaner for sure 👏🏼
@@ -2,7 +2,7 @@ | |||
* | |||
* Hedera JSON RPC Relay | |||
* | |||
* Copyright (C) 2024 Hedera Hashgraph, LLC | |||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC |
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.
Let's revert back to original
packages/config-service/tests/src/services/loggerService.spec.ts
Outdated
Show resolved
Hide resolved
packages/config-service/tests/src/services/validationService.spec.ts
Outdated
Show resolved
Hide resolved
public static get(name: ConfigKey): string | number | boolean | null | undefined { | ||
return this.getInstance().envs[name]; | ||
} |
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.
Hmm, this still doesn’t resolve the primary issue with the .get() method. There are still instances across the codebase where the error “Argument of type ‘string | number | boolean’ is not assignable to parameter of type…” persists, as shown in the example below. This remains an unresolved task that the ticket is meant to address.
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.
Luis adds a very good solution which can fix the issue, please refer to his solution here #3350 (comment)
…asting Signed-off-by: belloibrahv <[email protected]>
Quality Gate passedIssues Measures |
Hello @acuarica and @quiet-node, I’ve addressed the recent feedback, incorporating the suggested changes to enhance type safety and eliminate manual casting in |
Description:
This PR addresses type inconsistencies in the test specifications related to the
ConfigService.get(<envName>)
method. The changes ensure type safety and consistency across all affected test cases by aligning with the expected types defined in theConfigService.get()
method.Key Changes:
envName.ts
to define and manage environment variable types.envName
file, ensuring type consistency withConfigService.get()
.Related issue(s):
Fixes #3142
Notes for Reviewer:
ConfigService.get()
method's expected types.ConfigName
enum) were incorporated where feasible.Checklist: