-
Notifications
You must be signed in to change notification settings - Fork 0
Support Tracker Programs as output and support writing in different DEs #3
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
Support Tracker Programs as output and support writing in different DEs #3
Conversation
… Add Logger classes, change README and scripts to test
…kerProgramLoggerD2Repository
c5f5d76
to
525055e
Compare
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 @anagperal
From the architecture point of view, it is OK for me.
The unique thing pending is to avoid the use of as Promise<Logger>
I think that I have found a solution changing the signature of initLogger:
export async function initLogger(config: LoggerConfig): Promise<Logger<string>| Logger<TrackerProgramContent>> {
switch (config.type) {
case "program":
return ProgramLogger.create(config);
case "trackerProgram":
return TrackerProgramLogger.create(config);
case "console":
default:
return ConsoleLogger.create();
}
}
With this solution, is not necessary to pass generic from the outside of the library.
Wait to @tokland revision to receive the ok for this change
thanks @xurxodev ! I have tested this in the example scripts and it is giving me the following errors in logger.info and in logger.success (CC @tokland):
|
I'll check it tomorrow. |
Ok, I see what's going on. TS is right about the problem, In any case, we need a different approach. For TS to match a generic type argument, it has to be used in the function argument. Also, it's not idiomatic to pass explicit type parameters (unless parameter-less wrappers), TS should be able to infer it. Proposal: type LoggerType = {
program: ProgramLogger;
trackerProgram: TrackerProgramLogger;
console: ConsoleLogger;
};
export async function initLogger<Config extends LoggerConfig>(
config: Config
): Promise<LoggerType[Config["type"]]> {
type Res = Promise<LoggerType[Config["type"]]>;
switch (config.type) {
case "program":
return ProgramLogger.create(config) as Res;
case "trackerProgram":
return TrackerProgramLogger.create(config) as Res;
case "console":
return ConsoleLogger.create() as Res;
}
} And now there is no need to pass the type parameter, as it's inferred, 100% type-safe:
Note that we still need to do the |
thanks @tokland for the solution and the explanation of the problem, it works perfectly! 🎉 |
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 @anagperal
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.
Thx @anagperal !
📌 References
📝 Implementation
📹 Screenshots/Screen capture
Screencast.from.14-02-24.08.44.28.webm
🔥 Notes to the tester
Using script to just testing the logger (add corresponding
url
,auth
and the ids needed.message
is the Data Element Id of a Data Value of the event to be logged):yarn script-tracker-program-example --url "" --auth admin:district --program "" --tracked-entity "" --program-stage "" --enrollment "" --message "" --debug
Using yarn link to test logger in your app:
On another app:
$ yarn link "@eyeseetea/d2-logger"