Skip to content
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: replace global variable and refactor code #71

Merged
merged 3 commits into from
Oct 15, 2024
Merged

Conversation

zongqichen
Copy link
Contributor

No description provided.

Copy link
Member

@aramovic79 aramovic79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reported a few findings.
cc: @zongqichen @Fannon @swennemers

lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/ord.js Outdated Show resolved Hide resolved
lib/templates.js Show resolved Hide resolved
lib/templates.js Outdated Show resolved Hide resolved
@aramovic79
Copy link
Member

aramovic79 commented Oct 11, 2024

Putting two cents:

enums vs objects in javascript

You can find on many places that javascript developers prefer using Object.freeze instead of enum (or as const casting).

Enum can be defined as:

    const RESOURCE_TYPE = Object.freeze(
    {
        "api": "api",
        "event": "event"
    });

Please pay attention on Object.freeze. It serves to disallow changing of the object which at the end causes that RESOURCE_TYPE acts as an enum.

@aramovic79
Copy link
Member

I think that the code:

function _getPackageID(namespace, packageIds, apiOrEvent) {
    if (packageIds instanceof Set) {
        const packageArray = Array.from(packageIds);

        if (apiOrEvent === "api") {
            const apiPackage = packageArray.find((pkg) => pkg.includes("-api"));
            if (apiPackage) return apiPackage;
        } else if (apiOrEvent === "event") {
            const eventPackage = packageArray.find((pkg) => pkg.includes("-event"));
            if (eventPackage) return eventPackage;
        }

        return packageArray.find(pkg => pkg.includes(namespace));
    }
}

can be written this way:

function _getPackageID(namespace, packageIds, apiOrEvent) {
    return packageIds.find((pkg) => pkg.includes("-" + apiOrEvent)) || packageIds.find((pkg) => pkg.includes(namespace));
}

where I think that instead of apiOrEvent we should name this input parameter like resourceType

@zongqichen
Copy link
Contributor Author

I think that the code:

function _getPackageID(namespace, packageIds, apiOrEvent) {
    if (packageIds instanceof Set) {
        const packageArray = Array.from(packageIds);

        if (apiOrEvent === "api") {
            const apiPackage = packageArray.find((pkg) => pkg.includes("-api"));
            if (apiPackage) return apiPackage;
        } else if (apiOrEvent === "event") {
            const eventPackage = packageArray.find((pkg) => pkg.includes("-event"));
            if (eventPackage) return eventPackage;
        }

        return packageArray.find(pkg => pkg.includes(namespace));
    }
}

can be written this way:

function _getPackageID(namespace, packageIds, apiOrEvent) {
    return packageIds.find((pkg) => pkg.includes("-" + apiOrEvent)) || packageIds.find((pkg) => pkg.includes(namespace));
}

where I think that instead of apiOrEvent we should name this input parameter like resourceType

I create an issue regarding to this refactor: #73

"api": "api"
});

const COMPILER_TYPES = Object.freeze(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this constant as "CDS_COMPILE_TO" and also add the other targets: sql, hana, asyncapi, json, hana, edm, yaml and cdl. Just for the sake of completeness.

"edmx": "edmx"
});

const CONTENT_MERGE_KEY = 'ordId'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const CONTENT_MERGE_KEY = 'ordId'
const ORD_KEY = 'ordId'

break;
case COMPILER_TYPES.asyncapi2:
try {
responseFile = asyncapi(csn, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why is somewhere called imported function(like asyncapi here), and somewhere is used the whole cds.compile.to... statement. Is there some particular reason?

const fGeneratePaths = (srv, srvDefinition) => {
const srvObj = { name: srv, definition: srvDefinition };
const protocols = cds.service.protocols;
const generatePaths = (srv, srvDefinition) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private function, right? Maybe to name it _generatePath?
Also, when defining functions, on some places we use function and on the others const. Do we follow some rule when to use what? I couldn't find any regularity, it seems it was randomly/arbitrarily decided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As #57 discussed, we should use function

}),
{}
);
const readORDExtensions = (srv) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are calling this function in multiple places in a sequence when generating ord document and every time we have the same input parameter - serviceDefinition. Why not to call this function once when starting generating ORD document and then pass the result as an input parameter of all subsequent functions: _getGroups, _getApiResources, _getEventResources... ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's close this PR and fix this as part of the next PR #64

@zongqichen zongqichen merged commit 4897fd6 into main Oct 15, 2024
3 checks passed
@zongqichen zongqichen deleted the remove_global branch October 15, 2024 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants