1"use strict";(self.webpackChunkarchetype_docs=self.webpackChunkarchetype_docs||[]).push([[2461],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>h});var a=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function o(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},s=Object.keys(e);for(a=0;a<s.length;a++)n=s[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(a=0;a<s.length;a++)n=s[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var l=a.createContext({}),p=function(e){var t=a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},c=function(e){var t=p(e.components);return a.createElement(l.Provider,{value:t},e.children)},m="mdxType",u={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},d=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,s=e.originalType,l=e.parentName,c=o(e,["components","mdxType","originalType","parentName"]),m=p(n),d=r,h=m["".concat(l,".").concat(d)]||m[d]||u[d]||s;return n?a.createElement(h,i(i({ref:t},c),{},{components:n})):a.createElement(h,i({ref:t},c))}));function h(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var s=n.length,i=new Array(s);i[0]=d;var o={};for(var l in t)hasOwnProperty.call(t,l)&&(o[l]=t[l]);o.originalType=e,o[m]="string"==typeof e?e:r,i[1]=o;for(var p=2;p<s;p++)i[p]=n[p];return a.createElement.apply(null,i)}return a.createElement.apply(null,n)}d.displayName="MDXCreateElement"},9282:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>l,contentTitle:()=>i,default:()=>m,frontMatter:()=>s,metadata:()=>o,toc:()=>p});var a=n(7462),r=(n(7294),n(3905));const s={slug:"asset-access",title:"Accessing assets",authors:["gd"],tags:["asset","new feature"]},i=void 0,o={permalink:"/blog/asset-access",editUrl:"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/blog/2022-05-25-asset-access.md",source:"@site/blog/2022-05-25-asset-access.md",title:"Accessing assets",description:"We present an improvement in the way assets data are accessed in version 1.3.0.",date:"2022-05-25T00:00:00.000Z",formattedDate:"May 25, 2022",tags:[{label:"asset",permalink:"/blog/tags/asset"},{label:"new feature",permalink:"/blog/tags/new-feature"}],readingTime:2.105,hasTruncateMarker:!0,authors:[{name:"Guillaume Duhamel",title:"CTO Completium",url:"https://www.linkedin.com/in/guillaumeduhamel/",imageURL:"/img/blog/gd.jpeg",key:"gd"}],frontMatter:{slug:"asset-access",title:"Accessing assets",authors:["gd"],tags:["asset","new feature"]},prevItem:{title:"Shortcut evaluation",permalink:"/blog/shortcut-evaluation"},nextItem:{title:"Iterable big map",permalink:"/blog/iterable-big-map"}},l={authorsImageUrls:[void 0]},p=[{value:"Problems",id:"problems",level:3},{value:"Solution",id:"solution",level:3},{value:"Single field access",id:"single-field-access",level:3}],c={toc:p};
1function m(e){let{components:t,...n}=e;return(0,r.kt)("wrapper",(0,a.Z)({},c,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("p",null,"We present an improvement in the way ",(0,r.kt)("a",{parentName:"p",href:"/docs/asset"},"assets")," data are accessed in version ",(0,r.kt)("a",{parentName:"p",href:"/docs/installation"},(0,r.kt)("inlineCode",{parentName:"a"},"1.3.0")),"."),(0,r.kt)("h3",{id:"problems"},"Problems"),(0,r.kt)("p",null,"Until this version, the only way to access an asset data was the field accessor operator of the form ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/expressions/asset#ak--asset_keyaf"},(0,r.kt)("inlineCode",{parentName:"a"},"A[k].f")),", where ",(0,r.kt)("inlineCode",{parentName:"p"},"A")," is the asset collection, ",(0,r.kt)("inlineCode",{parentName:"p"},"k")," the asset key and ",(0,r.kt)("inlineCode",{parentName:"p"},"f")," the asset field."),(0,r.kt)("p",null,"The first problem was the access to multiple fields; for example, consider the following asset declaration:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},"asset vehicle {\n vin : string;\n manufacturer : string;\n year : nat;\n nbdoors : nat\n}\n")),(0,r.kt)("p",null,"Accessing several fields would end up accessing the underlying map several times:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const k = "1G1AF1F57A7192174";\nconst m = vehicle[k].manufacturer;\nconst y = vehicle[k].year;\nconst n = vehicle[k].nbdoors\n')),(0,r.kt)("p",null,"Here the test and fail instructions are repeated three times."),(0,r.kt)("p",null,"The second problem is the ",(0,r.kt)("em",{parentName:"p"},"implicit")," fail of the ",(0,r.kt)("inlineCode",{parentName:"p"},"[]")," operator in situations where it is implicit that the asset exists."),(0,r.kt)("p",null,"It is typically the case when the asset field is accessed in the ",(0,r.kt)("inlineCode",{parentName:"p"},"true")," branch of a ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/expressions/asset#acontainsk--asset_keya"},(0,r.kt)("inlineCode",{parentName:"a"},"contains"))," test:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const k = "1G1AF1F57A7192174";\nif vehicle.contains(k) then begin\n const m = vehicle[k].manufacturer;\n /* ... do something with m ... */\nend\n')),(0,r.kt)("p",null,"The ",(0,r.kt)("inlineCode",{parentName:"p"},"[].")," operator treats the case when asset is not found while it has already been tested."),(0,r.kt)("h3",{id:"solution"},"Solution"),(0,r.kt)("p",null,"Version ",(0,r.kt)("a",{parentName:"p",href:"/docs/installation"},(0,r.kt)("inlineCode",{parentName:"a"},"1.3.0"))," provides new operator ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/expressions/asset#ak--asset_keya"},(0,r.kt)("inlineCode",{parentName:"a"},"[]"))," that returns an ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/types#option%3CT%3E"},(0,r.kt)("inlineCode",{parentName:"a"},"option"))," of ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/types#asset_value%3CA%3E"},"asset value"),"."),(0,r.kt)("p",null,"Combined with the new ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/instructions/localvariable#-"},(0,r.kt)("inlineCode",{parentName:"a"},"?="))," declaration instruction, the proper way to retrieve all vehicle data presented above, is now as follows:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const v ?= vehicle["1G1AF1F57A7192174"];\nconst m = v.manufacturer;\nconst y = v.year;\nconst n = v.nbdoors\n')),(0,r.kt)("p",null,"The declaration of ",(0,r.kt)("inlineCode",{parentName:"p"},"v")," fails with ",(0,r.kt)("inlineCode",{parentName:"p"},'"OPTION_IS_NONE"')," if vehicle is not found. It is possible to specify an error message with:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const v ?= vehicle["1G1AF1F57A7192174"] : "VEHICLE_NOT_FOUND"\n')),(0,r.kt)("h3",{id:"single-field-access"},"Single field access"),(0,r.kt)("p",null,"Situations where a single asset field is accessed is very common though."),(0,r.kt)("p",null,"Version ",(0,r.kt)("a",{parentName:"p",href:"/docs/installation"},(0,r.kt)("inlineCode",{parentName:"a"},"1.3.0"))," provides a new operator ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/expressions/asset#ak--asset_keyaf"},(0,r.kt)("inlineCode",{parentName:"a"},"[]?."))," that returns an ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/types#option%3CT%3E"},(0,r.kt)("inlineCode",{parentName:"a"},"option"))," of field value, so that the non existence case may be ",(0,r.kt)("em",{parentName:"p"},"explicitely")," treated."),(0,r.kt)("p",null,"For example, the following retrieves an option of ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/types#nat"},(0,r.kt)("inlineCode",{parentName:"a"},"nat"))," to treat more specifically the case when the asset is not found:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const opt_n = vehicle["1G1AF1F57A7192174"]?.nbdoors;
1\n/* treat case when on is none ... */\n')),(0,r.kt)("p",null,(0,r.kt)("inlineCode",{parentName:"p"},"opt_n")," is typed ",(0,r.kt)("inlineCode",{parentName:"p"},"option<nat>")," and is ",(0,r.kt)("inlineCode",{parentName:"p"},"none")," if the vehicle is not found."),(0,r.kt)("p",null,"Operator ",(0,r.kt)("a",{parentName:"p",href:"/docs/reference/expressions/asset#ak--asset_keyaf"},(0,r.kt)("inlineCode",{parentName:"a"},"[]."))," is still available: it fails when asset is not found, which is syntactically convenient when there is an ",(0,r.kt)("em",{parentName:"p"},"implicit")," existence invariant on the asset:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-archetype"},'const n = vehicle["1G1AF1F57A7192174"].nbdoors;\n')),(0,r.kt)("p",null,"It implictely fails with ",(0,r.kt)("inlineCode",{parentName:"p"},'("ASSET_NOT_FOUND", "vehicle")')," if ",(0,r.kt)("inlineCode",{parentName:"p"},'"1G1AF1F57A7192174"')," is not found."))}m.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.