Overview of associative arrays
An associative array provides a mapping between a set of keys and values that the keys represent. A common example of such a mapping is a telephone book. A telephone book maps names of people in a jurisdiction to telephone numbers. Another common example is a dictionary. A dictionary maps terms to their definitions.
To expand on this concept, a telephone book contains a set of names. Each name in the set is a key, and each associated telephone number is a value. Using array-like notation, you can write:
telephonebook[peter] = 555-123-1234
telephonebook[shelly] = 555-234-2345
...
State typelist as a mapping index for an associative array of state
capitals:State typekey index |
Maps to... |
|---|---|
|
|
|
|
|
|
|
|
Two tasks are required to work with an associative array in Gosu:
- Exposing the key set to the type system
- Calculating the value from the key
Associative array mapping types
An associative array must have a key that maps to a value. The mapping type describes what PolicyCenter uses as the key and what value that key returns.
Mapping type |
Key |
Value |
More information |
|---|---|---|---|
Subtype mapping |
Entity subtype |
Implicit subtype field on an entity |
|
Typelist mapping |
Typelist |
Typekey field on the entity |
To implement an associative array,
add one of the following elements to an <array> element in the data
type definition file. The number of results that each returns—the cardinality
of the result set—depends on the element type.
|
Returns at most one element. The return type is an object of the type of the array. |
|
Returns an array of results that match the value. The number of results can be zero, one, or more. |
Each <array>
element in a data type definition file can have zero or one of each of
these elements.
As an example, in the ClaimCenter Claim definition file (), you see the following
XML (simplified for clarity):
<entity xmlns="http://guidewire.com/datamodel"
entity="Claim"
table="claim"
type="retireable">
...
<array arrayentity="ClaimMetric"
desc="Metrics related to this claim."
exportable="false"
ignoreforevents="true"
name="ClaimMetrics"
triggersValidation="false">
<link-association>
<subtype-map/>
</link-association>
<array-association>
<typelist-map field="ClaimMetricCategory"/>
</array-association>
</array>
...
</entity>
See also
Scriptability and associative arrays
It is possible to set the following attributes on each <link-association> and <array-association> element:
hasGetterhasSetter
For example:
<link-association hasGetter="true" hasSetter="true">
<typelist-map field="TAccountType"/>
</link-association>
For these attributes:
- If
hasGetteristrue, you can read the property. - If
hasSetteristrue, you can update the property.
hasGetter="true".See also
Setting array member values
There are several restrictions on setting associative array member values, including:
- You can use a query builder expression to retrieve a specific entity instance. However, the
result of the query is read-only. You must add the retrieved entity to a bundle to
be able to manipulate its fields. To work with bundles, use one of the
following:
var bundle = gw.transaction.Transaction.getCurrent() gw.transaction.Transaction.runWithNewBundle(\ bundle -> ) //Use this version in the Gosu tester - You can set array values only on fields that are database-backed fields, not on fields that are derived properties. To determine which fields are derived, consult the PolicyCenter Data Dictionary.
See also
