@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 3.27 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import t from"../../../Logger.js";import{setDeepValue as e}from"../../../object.js";import{getPropertiesMetadata as r}from"../../metadata.js";import{isCollection as o}from"./type.js";import{originSpecificReadPropertyDefinition as n}from"./utils.js";import{addUnsupportedMessage as i}from"../../../../webdoc/support/unsupportedErrors.js";const p=()=>t.getLogger("esri.core.accessorSupport.extensions.serializableProperty.reader");function u(t,r,o){t&&(!o&&!r.read||r.read?.reader||!1===r.read?.enabled||m(t)&&e("read.reader",s(t),r))}function s(t){const e=t.ndimArray??0;if(e>1)return a(t);if(1===e)return d(t);if("type"in t&&l(t.type)){const e=t.type.prototype?.itemType?.Type,r=d("function"==typeof e?{type:e}:{types:e});return(e,o,n)=>{const i=r(e,o,n);return i?new t.type(i):i}}return y(t)}function y(t){return"type"in t?f(t.type):v(t.types)}function f(t){return t.prototype.read?(e,r,o)=>{if(null==e)return e;const n=typeof e;if("object"!==n)return void p().error(`Expected JSON value of type 'object' to deserialize type '${t.prototype.declaredClass}', but got '${n}'`);const i=new t;return i.read(e,o),i}:t.fromJSON}function c(t,e,r,o){return 0!==o&&Array.isArray(e)?e.map(e=>c(t,e,r,o-1)):t(e,void 0,r)}function a(t){const e=y(t),r=c.bind(null,e),o=t.ndimArray??0;return(t,e,n)=>{if(null==t)return t;t=r(t,n,o);let i=o,p=t;for(;i>0&&Array.isArray(p);)i--,p=p[0];if(void 0!==p)for(let r=0;r<i;r++)t=[t];return t}}function d(t){const e=y(t);return(t,r,o)=>{if(null==t)return t;if(Array.isArray(t)){const r=[];for(const n of t){const t=e(n,void 0,o);void 0!==t&&r.push(t)}return r}const n=e(t,void 0,o);return void 0!==n?[n]:void 0}}function l(t){if(!o(t))return!1;const e=t.prototype.itemType;return!(!e||!e.Type)&&("function"==typeof e.Type?j(e.Type):g(e.Type))}function m(t){return"types"in t?g(t.types):j(t.type)}function j(t){return!Array.isArray(t)&&(!!t&&("object"==typeof t||"function"==typeof t)&&t.prototype&&("read"in t.prototype||"fromJSON"in t||l(t)))}function g(t){for(const e in t.typeMap){if(!j(t.typeMap[e]))return!1}return!0}function v(t){let e=null;const r=t.errorContext??"type",o=t.validate;return(n,u,s)=>{if(null==n)return n;const y=typeof n;if("object"!==y)return void p().error(`Expected JSON value of type 'object' to deserialize, but got '${y}'`);e||(e=A(t));const f=t.key;if("string"!=typeof f)return;const c=n[b(f,e,s)],a=c?e[c]:t.defaultKeyValue?t.typeMap[t.defaultKeyValue]:void 0;if(!a){const t=`Type '${c||"unknown"}' is not supported`;return s?.messages&&n&&i(s,"warning",r,t,{definition:n,context:s}),void p().error(t)}const d=new a;return d.read(n,s),o?o(d):d}}function b(t,e,o){const i=Object.values(e)[0],p=r(i.prototype)[t],u=n(p,o),s=u?.read?.source;return s&&"string"==typeof s?s:t}function A(t){const e={};for(const o in t.typeMap){const n=t.typeMap[o],i=r(n.prototype);if("function"==typeof t.key)continue;const p=i[t.key];if(!p)continue;if(p.json?.type&&Array.isArray(p.json.type)&&1===p.json.type.length&&"string"==typeof p.json.type[0]){e[p.json.type[0]]=n;continue}const u=p.json?.write;if(!u?.writer){e[o]=n;continue}const s=u.target,y="string"==typeof s?s:t.key,f={};u.writer(o,f,y),f[y]&&(e[f[y]]=n)}return e}export{u as create,s as createTypeReader};