1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[35325],{23464:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>a,default:()=>h,frontMatter:()=>o,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"rules/style/mixed-iteration","title":"mixed-iteration","description":"Summary: Mixed iteration style","source":"@site/projects/regal/rules/style/mixed-iteration.md","sourceDirName":"rules/style","slug":"/rules/style/mixed-iteration","permalink":"/projects/regal/rules/style/mixed-iteration","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"autoSidebar","previous":{"title":"messy-rule","permalink":"/projects/regal/rules/style/messy-rule"},"next":{"title":"no-whitespace-comment","permalink":"/projects/regal/rules/style/no-whitespace-comment"}}');var t=i(74848),s=i(28453);const o={},a="mixed-iteration",l={},c=[{value:"Rationale",id:"rationale",level:2},{value:"Configuration Options",id:"configuration-options",level:2},{value:"Related Resources",id:"related-resources",level:2}];function d(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,s.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.header,{children:(0,t.jsx)(n.h1,{id:"mixed-iteration",children:"mixed-iteration"})}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Summary"}),": Mixed iteration style"]}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Category"}),": Style"]}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Avoid"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:"package policy\n\nallow if {\n # mixing 'some .. in' and reference iteration\n some resource in input.assets[_]\n\n # do something with resource\n}\n"})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Prefer"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:"package policy\n\nallow if {\n # using 'some .. in' iteration consistently\n some asset in input.assets\n some resource in asset\n\n # do something with resource\n}\n\n# alternatively\n\nallow if {\n # using reference iteration consistently\n resource := input.assets[_][_]\n\n # do something with resource\n}\n"})}),"\n",(0,t.jsx)(n.h2,{id:"rationale",children:"Rationale"}),"\n",(0,t.jsxs)(n.p,{children:["Using ",(0,t.jsx)(n.code,{children:"some .. in"})," is often the ",(0,t.jsx)(n.a,{href:"https://www.openpolicyagent.org/projects/regal/rules/style/prefer-some-in-iteration",children:"best choice"}),' for\niteration in modern Rego, as it clearly communicates what\'s going on and which variables will be bound in each\niteration. An alternative approach is to place variables (including the special "wildcard" variable ',(0,t.jsx)(n.code,{children:"_"}),') in parts of a\nreferences, which unless assigned elsewhere automatically will be bound to every possible value in the collection being\ntraversed (often called "output variables").']}),"\n",(0,t.jsx)(n.p,{children:'"Reference style" iteration is often preferred when deeply nested structures are traversed, as they allow\nexpressing that in a very concise (and sometimes, more performant) manner.'}),"\n",(0,t.jsxs)(n.p,{children:["While both forms of iteration are valid and have their place, mixing both forms in a single iteration is arguably\nconfusing. Feel free to choose either ",(0,t.jsx)(n.code,{children:"some .. in"})," or reference style depending on your preference (and when in doubt,\nuse ",(0,t.jsx)(n.code,{children:"some .. in"}),"), but don't mix the two different styles in a single iteration expression."]}),"\n",(0,t.jsx)(n.h2,{id:"configuration-options",children:"Configuration Options"}),"\n",(0,t.jsxs)(n.p,{children:["This linter rule provides the following ",(0,t.jsx)(n.a,{href:"https://www.openpolicyagent.org/projects/regal/configuration",children:"configuration options"}),":"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",metastring:'title=".regal/config.yaml or .regal.yaml"',children:'rules:\n style:\n mixed-iteration:\n # one of "error", "warning", "ignore"\n level: error\n'})}),"\n",(0,t.jsx)(n.h2,{id:"related-resources",children:"Related Resources"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["Regal Docs: ",(0,t.jsx)(n.a,{href:"https://www.openpolicyagent.org/projects/regal/rules/style/prefer-some-in-iteration",children:"prefer-some-in-iteration"})]}),"\n",(0,t.jsxs)(n.li,{children:["OPA Docs: ",(0,t.jsx)(n.a,{href:"https://www.openpolicyagent.org/docs/policy-language/#membership-and-iteration-in",children:"Membership and iteration"})]}),"\n",(0,t.jsxs)(n.li,{children:["GitHub: ",(0,t.jsx)(n.a,{href:"https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/style/mixed-iteration/mixed_iteration.rego",children:"Source Code"})]}),"\n"]})]})}function h(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},28453:(e,n,i)=>{i.d(n,{R:()=>o,x:()=>a});var r=i(96540);const t={},s=r.createContext(t);function o(e){const n=r.useContext(s);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:o(e.components),r.createElement(s.Provider,{value:n},e.children)}}}]);
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.