Extend the current environment with additional options to produce a new Env. Note, the extended Env value should not share memory with the original. It is possible, however, that a CustomTypeAdapter or CustomTypeProvider options could provide values which are mutable. To ensure separation of state
(opts ...EnvOption)
| 596 | // TypeProvider are immutable, or that their underlying implementations are based on the |
| 597 | // ref.TypeRegistry which provides a Copy method which will be invoked by this method. |
| 598 | func (e *Env) Extend(opts ...EnvOption) (*Env, error) { |
| 599 | if _, chkErr := e.getCheckerOrError(); chkErr != nil { |
| 600 | return nil, chkErr |
| 601 | } |
| 602 | |
| 603 | prsrOptsCopy := slices.Clone(e.prsrOpts) |
| 604 | chkOptsCopy := slices.Clone(e.chkOpts) |
| 605 | varsCopy := slices.Clone(e.variables) |
| 606 | macsCopy := slices.Clone(e.macros) |
| 607 | progOptsCopy := slices.Clone(e.progOpts) |
| 608 | validatorsCopy := slices.Clone(e.validators) |
| 609 | costOptsCopy := slices.Clone(e.costOptions) |
| 610 | |
| 611 | // Copy the adapter / provider if they appear to be mutable. |
| 612 | adapter := e.adapter |
| 613 | provider := e.provider |
| 614 | adapterReg, isAdapterReg := e.adapter.(*types.Registry) |
| 615 | providerReg, isProviderReg := e.provider.(*types.Registry) |
| 616 | // In most cases the provider and adapter will be a ref.TypeRegistry; |
| 617 | // however, in the rare cases where they are not, they are assumed to |
| 618 | // be immutable. Since it is possible to set the TypeProvider separately |
| 619 | // from the TypeAdapter, the possible configurations which could use a |
| 620 | // TypeRegistry as the base implementation are captured below. |
| 621 | if isAdapterReg && isProviderReg { |
| 622 | reg := providerReg.Copy() |
| 623 | provider = reg |
| 624 | // If the adapter and provider are the same object, set the adapter |
| 625 | // to the same ref.TypeRegistry as the provider. |
| 626 | if adapterReg == providerReg { |
| 627 | adapter = reg |
| 628 | } else { |
| 629 | // Otherwise, make a copy of the adapter. |
| 630 | adapter = adapterReg.Copy() |
| 631 | } |
| 632 | } else if isProviderReg { |
| 633 | provider = providerReg.Copy() |
| 634 | } else if isAdapterReg { |
| 635 | adapter = adapterReg.Copy() |
| 636 | } |
| 637 | |
| 638 | ext := &Env{ |
| 639 | parent: e, |
| 640 | Container: e.Container, |
| 641 | variables: varsCopy, |
| 642 | functions: e.functions, |
| 643 | macros: macsCopy, |
| 644 | contextProto: e.contextProto, |
| 645 | progOpts: progOptsCopy, |
| 646 | adapter: adapter, |
| 647 | features: e.features, |
| 648 | limits: e.limits, |
| 649 | appliedFeatures: e.appliedFeatures, |
| 650 | libraries: e.libraries, |
| 651 | validators: validatorsCopy, |
| 652 | provider: provider, |
| 653 | chkOpts: chkOptsCopy, |
| 654 | prsrOpts: prsrOptsCopy, |
| 655 | costOptions: costOptsCopy, |