-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
Add missing methods that help evaluate if given collection is empty or not #88
Comments
@magicmatatjahu go for it! |
I started looking at the code and noticed that collections are often returned as empty objects or empty arrays, even if the field is not defined, e.g. tags field, like here https://github.com/asyncapi/parser-js/blob/master/lib/models/asyncapi.js#L136 In my opinion it should return null if field is not defined, and also in case if collection (array or object) is empty (zero indexes in array and zero keys in object). For primitives, like string, numbers etc. if field is defined and empty (only for empty string) the method should return null, otherwise value. I don't know what impact it will have on e.g. html template, with a change from an empty array to null, but I could improve it everywhere. Also I tried apply docs for const extensionError = 'Extension key is not prefixed. Please ensure you prefix it with `x-`.';
/**
* Implements functions to deal with the SpecificationExtensions object.
* @mixin MixinSpecificationExtensions
*/
const MixinSpecificationExtensions = {
/**
* @returns {boolean}
*/
hasExtensions() {
return !!this.extensions();
},
/**
* @returns {(Object<string, Any>|null)}
*/
extensions() {
const result = {};
Object.entries(this._json).forEach(([key, value]) => {
if ((/^x-[\w\d\.\-\_]+$/).test(key)) {
result[String(key)] = value;
}
});
return Object.keys(result).length ? result : null;
},
/**
* @param {string} key - Extension key. Must start with `x-` prefix. Otherwise function throw an error.
* @returns {Any}
*/
extension(key) {
if (!key.startsWith('x-')) {
throw new Error(extensionError);
};
return this._json[String(key)];
},
/**
* @param {string} key - Extension key. Must start with `x-` prefix. Otherwise throw an error.
* @returns {Any}
*/
ext(key) {
return this.extension(key);
},
};
module.exports = MixinSpecificationExtensions;
// Some model
const { mix } = require('../utils');
const Base = require('./base');
const MixinSpecificationExtensions = require('../mixins/specification-extensions');
/**
* Implements functions to deal with the License object.
* @class License
* @extends Base
* @mixes MixinSpecificationExtensions
* @returns {License}
*/
class License extends Base {
/**
* @returns {string}
*/
name() {
return this._json.name;
}
/**
* @returns {boolean}
*/
hasUrl() {
return !!this._json.url;
}
/**
* @returns {(string|null)}
*/
url() {
return this._json.url || null;
}
}
module.exports = mix(License, MixinSpecificationExtensions); And as you can see, it works! I created mixins for common types in models like I could make PR on the next week, but I won't hide it, it will be quite big for review, because I found a lot of errors and by adding missing tests, turned out that some things throw exceptions. Divide it into two or three or rather leave it all in one? |
Good investigation, I like the idea of
PR would be great but as small as possible. We should not mix this PR with subject of Regarding what you saw, that sometimes instead of I know common practice is to return @jonaslagoni you wrote some code for templates, what is your opinion here? |
I think empty arrays from a templaters point of view is better, because we can directly use the variable without checking it as you say @derberg. The less code in the templates the better 😄 But I can understand why |
utils.mix = (clazz, ...mixins) => {
mixins.forEach(mixin => Object.assign(clazz.prototype, mixin));
return clazz;
} and then we also must use I can consolidate all models with the null problem in another PR, but we must agree on how it should look. At the moment, what you wrote guys:
But what with primitives and custom types? My suggestions:
What do you think? Btw. Thanks for the activity in the issue :) |
For me, the thing about returning empty arrays, returning
If, and only if, the answers to the questions above is "no", then I'll go for empty arrays for better UX. Otherwise, I'll go for its raw value ( And regarding adding more methods, I'd also consider adding |
This issue has been automatically marked as stale because it has not had recent activity 😴 |
duplicate #173 |
Reason/Context
As discussed here #85 (comment) we need more functions like
hasMessages
orhasSchemas
in the components model, but it would be also nice to analyze other places.Description
Add to components model functions like
hasMessages
,hasSchemas
,hasSecuritySchemas
The text was updated successfully, but these errors were encountered: