This commit is contained in:
Like Zhu 2021-07-29 16:51:12 -07:00
Родитель f6da6b51f9
Коммит b0e1bf25d0
4 изменённых файлов: 63 добавлений и 42 удалений

Просмотреть файл

@ -0,0 +1,37 @@
const _ = require('underscore');
function castPath(value, object) {
if (_.isArray(value)) {
return value;
}
if (_.has(object, value)) {
return [value];
}
return _.compact(value.split(/[[\].]/));
}
function get(object, path, defaultValue) {
let sub;
let i;
const keyPath = castPath(path, object);
for (i = 0, sub = object; i < keyPath.length; i += 1) {
if (!_.isObject(sub)) {
return defaultValue;
}
sub = sub[keyPath[i]];
if (_.isUndefined(sub)) {
return defaultValue;
}
}
return sub;
}
module.exports = {
get,
};

Просмотреть файл

Просмотреть файл

@ -3,6 +3,7 @@
*/
const _ = require('underscore');
const { get } = require('./lodash');
const { Registry } = require('./registry');
const {
defineConstProperty,
@ -141,12 +142,10 @@ module.exports = (edm, {
_.each(_.keys(acts), key => {
const typeName = getCallableTypeName(key);
const parameters = {};
Object.keys(acts[key].Parameter || {}).forEach(k => {
const parameter = acts[key].Parameter[k];
parameters[k] = getTypeName([], parameter);
});
const parameters = _.mapObject(
acts[key].Parameter,
parameter => getTypeName([], parameter),
);
actions.push(new ActionType({
name: typeName,
@ -158,12 +157,10 @@ module.exports = (edm, {
_.each(_.keys(funcs), key => {
const typeName = getCallableTypeName(key);
const parameters = {};
Object.keys(funcs[key].Parameter || {}).forEach(k => {
const param = funcs[key].Parameter[k];
parameters[k] = getTypeName([], param);
});
const parameters = _.mapObject(
funcs[key].Parameter,
parameter => getTypeName([], parameter),
);
functions.push(new FunctionType({
name: typeName,
@ -191,14 +188,9 @@ module.exports = (edm, {
if (!edm.types.resolve(qualifiedName)) {
const dependencies = [];
const properties = {};
Object.keys(schema.properties || {}).forEach(key => {
const typeInfo = schema.properties[key];
properties[key] = {
typeName: getTypeName(dependencies, typeInfo),
};
});
const properties = _.mapObject(schema.properties, typeInfo => ({
typeName: getTypeName(dependencies, typeInfo),
}));
const type = new EntityType({
name: qualifiedName,
@ -212,14 +204,14 @@ module.exports = (edm, {
_.each(dependencies, defineSchemaType);
const entityActions = _.get(schema, '$$ODataExtension.Action', null);
const entityFunctions = _.get(schema, '$$ODataExtension.Function', null);
const entityActions = get(schema, '$$ODataExtension.Action', null);
const entityFunctions = get(schema, '$$ODataExtension.Function', null);
if (entityActions || entityFunctions) {
defineCallableOnType(entityActions, entityFunctions, type, qualifiedName);
}
const collCallable = _.get(schema, '$$ODataExtension.Collection', null);
const collCallable = get(schema, '$$ODataExtension.Collection', null);
if (collCallable) {
defineCallableOnType(

Просмотреть файл

@ -219,18 +219,13 @@ module.exports = edm => {
function compileProperties(properties) {
const { namespace } = this;
const ret = {};
Object.keys(properties || {}).forEach(name => {
const { typeName } = properties[name];
ret[name] = new Property({
name,
typeName,
namespace,
});
});
return ret;
return _.mapObject(properties, ({
typeName,
}, name) => new Property({
name,
typeName,
namespace,
}));
}
/**
@ -397,13 +392,10 @@ module.exports = edm => {
function compileParameters(parameters) {
const { namespace } = this;
const ret = {};
Object.keys(parameters || {}).forEach(name => {
const { typeName } = parameters[name];
ret[name] = new Parameter({ name, namespace, typeName });
});
return ret;
return _.mapObject(
parameters,
({ typeName }, name) => new Parameter({ name, namespace, typeName }),
);
}
/**