pacc 4.29.1 → 4.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -79,28 +79,30 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
79
79
  * [priority\_attribute](#priority_attribute)
80
80
  * [timeout\_attribute](#timeout_attribute)
81
81
  * [language\_attribute](#language_attribute)
82
- * [expand](#expand)
82
+ * [environmentValues](#environmentvalues)
83
83
  * [Parameters](#parameters-3)
84
- * [filter](#filter)
84
+ * [expand](#expand)
85
85
  * [Parameters](#parameters-4)
86
- * [setAttributes](#setattributes)
86
+ * [filter](#filter)
87
87
  * [Parameters](#parameters-5)
88
- * [getAttributes](#getattributes)
88
+ * [setAttributes](#setattributes)
89
89
  * [Parameters](#parameters-6)
90
+ * [getAttributes](#getattributes)
91
+ * [Parameters](#parameters-7)
90
92
  * [tokens](#tokens)
91
93
  * [tokens](#tokens-1)
92
- * [Parameters](#parameters-7)
93
- * [setAttribute](#setattribute)
94
94
  * [Parameters](#parameters-8)
95
- * [getAttribute](#getattribute)
95
+ * [setAttribute](#setattribute)
96
96
  * [Parameters](#parameters-9)
97
- * [getAttributeAndOperator](#getattributeandoperator)
97
+ * [getAttribute](#getattribute)
98
98
  * [Parameters](#parameters-10)
99
+ * [getAttributeAndOperator](#getattributeandoperator)
100
+ * [Parameters](#parameters-11)
99
101
  * [lookup](#lookup)
100
102
  * [Token](#token)
101
103
  * [Properties](#properties-1)
102
104
  * [createToken](#createtoken)
103
- * [Parameters](#parameters-11)
105
+ * [Parameters](#parameters-12)
104
106
  * [PLUS](#plus)
105
107
  * [MINUS](#minus)
106
108
  * [STAR](#star)
@@ -184,6 +186,7 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
184
186
  * `values` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\<any>?** allowed values
185
187
  * `externalName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** attrubute name used by external system
186
188
  * `env` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)> | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))?** environment variable(s) used to provide the value
189
+ * `additionalValues` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** other values to be set in case our attribute is set
187
190
 
188
191
  ## default\_attribute
189
192
 
@@ -374,6 +377,18 @@ Type: [AttributeDefinition](#attributedefinition)
374
377
 
375
378
  Type: [AttributeDefinition](#attributedefinition)
376
379
 
380
+ ## environmentValues
381
+
382
+ Extract values from environment.
383
+
384
+ ### Parameters
385
+
386
+ * `attributes` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** as from process.env
387
+ * `env` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** as from process.env
388
+ * `instanceIdentifier` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** part of variable name.
389
+
390
+ Returns **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))** undefined if no suitable environment variables have been found
391
+
377
392
  ## expand
378
393
 
379
394
  Expand expressions inside of object graphs.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.29.1",
3
+ "version": "4.31.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -1,3 +1,5 @@
1
+ import {getAttribute} from "./settergetter.mjs";
2
+
1
3
  export const baseTypes = new Set(["string", "number", "bigint", "boolean"]);
2
4
 
3
5
  export const types = {
@@ -110,3 +112,16 @@ export function prepareValue(value, attribute) {
110
112
  }
111
113
  return value;
112
114
  }
115
+
116
+ export function manadatoryAttributesPresent(object, attributes) {
117
+ for (const [path, attribute] of attributeIterator(
118
+ attributes,
119
+ (name, attribute) => attribute.mandatory
120
+ )) {
121
+ const name = path.join(".");
122
+ if (getAttribute(object, name) === undefined) {
123
+ return false;
124
+ }
125
+ }
126
+ return true;
127
+ }
@@ -12,9 +12,11 @@
12
12
  * @property {any} [default] the default value
13
13
  * @property {Function} [set] set the value
14
14
  * @property {Function} [get] get the value can be used to calculate default values
15
+ * @property {Function} [prepareValue]
15
16
  * @property {Set<any>} [values] allowed values
16
17
  * @property {string} [externalName] attrubute name used by external system
17
18
  * @property {string[]|string} [env] environment variable(s) used to provide the value
19
+ * @property {object} [additionalValues] other values to be set in case our attribute is set
18
20
  */
19
21
 
20
22
  /**
@@ -0,0 +1,34 @@
1
+ import { attributeIterator, setAttribute } from "pacc";
2
+
3
+ /**
4
+ * Extract values from environment.
5
+ * @param {Object} env as from process.env
6
+ * @param {Object} attributes as from process.env
7
+ * @param {string} instanceIdentifier part of variable name.
8
+ * @return {Object|undefined} undefined if no suitable environment variables have been found
9
+ */
10
+ export function environmentValues(env, attributes, instanceIdentifier) {
11
+ let values;
12
+
13
+ for (const [path, attribute] of attributeIterator(
14
+ attributes,
15
+ (name, attribute) => attribute.env || attribute.attributes
16
+ )) {
17
+ const name = path.join(".");
18
+
19
+ for (const envName of (Array.isArray(attribute.env)
20
+ ? attribute.env
21
+ : [attribute.env]
22
+ ).map(
23
+ name =>
24
+ name && name.replace("{{instanceIdentifier}}", () => instanceIdentifier)
25
+ )) {
26
+ if (env?.[envName] !== undefined) {
27
+ values ??= {};
28
+ setAttribute(values, name, env[envName]);
29
+ }
30
+ }
31
+ }
32
+
33
+ return values;
34
+ }
package/src/module.mjs CHANGED
@@ -6,6 +6,7 @@ export * from "./common-attributes.mjs";
6
6
  export * from "./properties.mjs";
7
7
  export * from "./expression.mjs";
8
8
  export * from "./expand.mjs";
9
+ export * from "./environment.mjs";
9
10
  export {
10
11
  setAttribute,
11
12
  getAttribute,
@@ -14,7 +14,10 @@ export function definePropertiesFromAttributes(
14
14
  const name = path.join(".");
15
15
  const externalName = attribute.externalName ?? name;
16
16
 
17
- let value = getAttribute(initialValues, externalName, attribute) ?? initialValues?.[externalName] ?? attribute.default;
17
+ let value =
18
+ getAttribute(initialValues, externalName, attribute) ??
19
+ initialValues?.[externalName] ??
20
+ attribute.default;
18
21
 
19
22
  if (value !== undefined) {
20
23
  if (path.length === 1) {
@@ -15,6 +15,7 @@ export function prepareAttributesDefinitions(newDefinitions: any, presentDefinit
15
15
  export function attributeIterator(definition: any, filter: Function, path?: string[]): Iterable<[string[], object]>;
16
16
  export function writableAttributeIterator(definition: any): Generator<[string[], any], void, any>;
17
17
  export function prepareValue(value: any, attribute: any): any;
18
+ export function manadatoryAttributesPresent(object: any, attributes: any): boolean;
18
19
  export const baseTypes: Set<string>;
19
20
  export const types: {
20
21
  base: {
@@ -12,9 +12,11 @@
12
12
  * @property {any} [default] the default value
13
13
  * @property {Function} [set] set the value
14
14
  * @property {Function} [get] get the value can be used to calculate default values
15
+ * @property {Function} [prepareValue]
15
16
  * @property {Set<any>} [values] allowed values
16
17
  * @property {string} [externalName] attrubute name used by external system
17
18
  * @property {string[]|string} [env] environment variable(s) used to provide the value
19
+ * @property {object} [additionalValues] other values to be set in case our attribute is set
18
20
  */
19
21
  /**
20
22
  * Common attribute properties.
@@ -39,9 +41,11 @@ export namespace string_collection_attribute {
39
41
  export { _default as default };
40
42
  export let set: Function;
41
43
  export let get: Function;
44
+ export let prepareValue: Function;
42
45
  export let values: Set<any>;
43
46
  export let externalName: string;
44
47
  export let env: string[] | string;
48
+ export let additionalValues: object;
45
49
  }
46
50
  /**
47
51
  * @type {AttributeDefinition}
@@ -194,6 +198,7 @@ export type AttributeDefinition = {
194
198
  * get the value can be used to calculate default values
195
199
  */
196
200
  get?: Function;
201
+ prepareValue?: Function;
197
202
  /**
198
203
  * allowed values
199
204
  */
@@ -206,5 +211,9 @@ export type AttributeDefinition = {
206
211
  * environment variable(s) used to provide the value
207
212
  */
208
213
  env?: string[] | string;
214
+ /**
215
+ * other values to be set in case our attribute is set
216
+ */
217
+ additionalValues?: object;
209
218
  };
210
219
  export { default_attribute as string_attribute, default_attribute_writable as string_attribute_writable, default_attribute as type_attribute, default_attribute_writable as state_attribute, boolean_attribute_writable_false as boolean_attribute, boolean_attribute_writable_true as active_attribute, secret_attribute as username_attribute, secret_attribute as password_attribute, secret_attribute as token_attribute, secret_attribute as certificate_attribute, integer_attribute as count_attribute, integer_attribute as size_attribute, default_attribute_writable as body_attribute, number_attribute as duration_attribute };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Extract values from environment.
3
+ * @param {Object} env as from process.env
4
+ * @param {Object} attributes as from process.env
5
+ * @param {string} instanceIdentifier part of variable name.
6
+ * @return {Object|undefined} undefined if no suitable environment variables have been found
7
+ */
8
+ export function environmentValues(env: any, attributes: any, instanceIdentifier: string): any | undefined;
@@ -6,4 +6,5 @@ export * from "./common-attributes.mjs";
6
6
  export * from "./properties.mjs";
7
7
  export * from "./expression.mjs";
8
8
  export * from "./expand.mjs";
9
+ export * from "./environment.mjs";
9
10
  export { setAttribute, getAttribute, getAttributeAndOperator } from "./settergetter.mjs";