Core evaluation module
The core evaluation module is a separately versioned Go library that provides the shared foundation for feature-flag decisions. It defines the flag model, evaluation contract, context, result and error signals, and conversion helpers so SDKs and other runtimes can apply the same decision rules.
Module boundary
The core module is a library boundary rather than a service. A caller supplies a flag definition and an evaluation context. The module returns a typed decision with its reason and any error state. Higher-level systems load flag data, expose the result, and record events. The root SDK uses the released module version declared in go.mod rather than treating the core code as an unversioned part of the SDK.
The root module depends on github.com/thomaspoignant/go-feature-flag/modules/core at version v0.8.2. This separate version makes the evaluation contract a compatibility boundary for the root SDK. The core module has its own Go module file and direct dependencies. Its rule dependency is replaced by the maintained github.com/go-feature-flag/rules fork so rule parsing and version comparison remain consistent with the project.
The core module keeps common evaluation behavior consistent across the product. Its main package roles are:
| Package | Role in evaluation |
|---|---|
flag | Describes flag values, targeting, rollouts, state, and decision details. |
evaluation | Applies the evaluation contract and produces a typed result. |
ffcontext | Carries the subject identity and attributes used by targeting. |
model | Defines the result returned to callers. |
internalerror | Separates a rule that does not match from a real evaluation error. |
dto | Connects external flag data to the core flag model. |
utils | Keeps context conversion, query preparation, type checks, and stable bucketing consistent. |
The core module does not fetch configuration or send events. Higher-level systems own those actions. The flag evaluation and targeting, evaluation contexts, WASM evaluation runtime, and OpenFeature provider support pages describe the surrounding consumers and boundaries.
References: go.mod• L1-3 go.mod• L68-72 modules/…/go.mod• L1-15
Evaluation foundation
The evaluation foundation joins three inputs: a stored flag definition, a context that describes the subject, and a request that states the expected value type and safe default. The flag chooses a variation. The evaluation layer checks that the choice fits the request and returns the value with the decision details.
Evaluation contract
The evaluation contract keeps callers independent from one concrete flag representation. It accepts the flag interface, flag key, evaluation context, request context, expected type, and SDK default. The flag interface exposes only the decision data that the evaluator needs, such as the selected value, version, event setting, variation lookup, and metadata.
The request context carries data for the current evaluation rather than data stored in the flag. This lets callers add shared context values or control data without changing the flag definition.
References: modules/…/evaluation.go• L16-66 modules/…/flag.go• L7-32 modules/…/context.go• L3-21
Evaluation sequence
The evaluation path turns a flag definition and a subject context into a typed result. It preserves the reason for the decision so downstream code can use the value and still understand how the value was chosen.
This diagram shows how the core inputs become a result.
flowchart LR
A[Flag definition] --> D[Flag decision]
B[Evaluation context] --> D
C[Evaluation request] --> D
D --> E[Selected variation and details]
E --> F[Typed result]
G[SDK default] --> F
The flag applies active rollouts and context enrichment before it evaluates its targeting rules. It uses the first matching targeting rule and then falls back to the default rule. If the selected value does not match the requested type, the result uses the SDK default and reports a type error. Rules in one evaluation share the prepared query context, which keeps the context view consistent across query formats and avoids repeated conversion work.
References: modules/…/internal_flag.go• L63-129 modules/…/rule.go• L114-139 modules/…/evaluation.go• L23-66
Flag model
The flag model describes the possible values and the conditions that select them. It gives evaluation one common view of variations, targeting rules, defaults, rollouts, state, version data, event settings, and metadata.
Flag definitions
Variations are the named values that a flag can return. A usable flag has compatible variation types and a valid default rule. The flag contract also tells callers whether the flag is disabled, whether evaluation events are enabled, which version is active, and what metadata belongs to the flag.
External configuration enters through a DTO, or data transfer object. The DTO conversion creates the internal flag form before evaluation and can convert the form back when another boundary needs the configuration shape. This separates the input format from the model used by evaluation runtimes.
References: modules/…/internal_flag.go• L19-61 modules/…/internal_flag.go• L286-340 modules/…/flag.go• L15-32 modules/…/dto.go• L7-60 modules/…/converter.go• L7-51
Targeting rules
Targeting rules decide which subjects receive a variation. A rule can select a named variation, split subjects by percentage, or increase a progressive rollout over time. Each non-default rule has a query, and the first rule that matches wins.
Queries can use JSONLogic or Nikunjy rules. The evaluator presents the context as a query map and reuses that view while it checks the rules. Percentage and progressive choices use a stable hash of the flag name and bucketing key, so a subject stays in the same bucket while the relevant configuration stays the same.
References: modules/…/rule.go• L19-24 modules/…/rule.go• L68-139 modules/…/rule.go• L168-209 modules/…/serializer.go• L7-20 modules/…/hash.go• L5-18
Time-based rollouts
Time-based rollouts change which flag behavior applies at a given date. Experimentation limits the period in which a flag behavior is active. Scheduled rollout steps change selected flag fields as time moves forward. Progressive rollout changes the share of subjects that receive its end variation between two dates.
The normal evaluation date is the current time. A protected context value can provide a controlled date for replay or test of a rollout. Scheduled steps apply in date order, so a later valid step can replace an earlier change to the same field.
References: modules/…/rollout.go• L3-13 modules/…/rollout_experimentation.go• L7-13 modules/…/rollout_progressive.go• L7-25 modules/…/rollout_scheduled.go• L7-11 modules/…/internal_flag.go• L75-107 modules/…/internal_flag.go• L207-276 modules/…/internal_flag.go• L500-505
Evaluation contexts
An evaluation context describes the subject of a decision and the facts that targeting can inspect. Its required identity is the targeting key. Custom attributes add subject or request data, while the anonymous state tells rules whether the subject has a known identity.
Context identity and attributes
ffcontext.Context supplies the targeting key, anonymous state, and custom attributes to the evaluator. Callers can create it directly or use a builder. The same context can also be converted to a map for query evaluation and serialization. That map exposes both key and targetingKey so supported query forms can use their expected field name.
The targeting key supports direct matching and stable percentage assignment. A flag can instead name a nested custom attribute as its bucketing key. Rules that do not split traffic can work without a key, but percentage and progressive rules need a usable key.
References: modules/…/context.go• L12-23 modules/…/context.go• L28-57 modules/…/context.go• L71-107 modules/…/serializer.go• L7-20 modules/…/internal_flag.go• L412-489
Protected context data
The context can carry GO Feature Flag control data in a protected namespace. This data is separate from normal targeting attributes. It can provide an evaluation date, a set of flags for bulk work, and metadata for event export.
The core module only reads these values. Higher-level components decide how to perform bulk evaluation or event handling. This keeps request control data available without coupling the evaluator to a service or exporter.
References: modules/…/goff_context_specifics.go• L5-12 modules/…/goff_context_specifics.go• L14-50 modules/…/context.go• L110-126
Result and error model
The result model returns the selected value together with the information needed to interpret it. Callers can use the safe value while also deciding whether to log, report, cache, or expose a failed decision.
Resolution details
Resolution details record how a flag made its choice. They can identify the selected variation, resolution reason, error code and message, matching rule, cache permission, and flag metadata. The evaluator adds the typed value and SDK-level tracking fields before returning the result.
Resolution reasons follow the project’s OpenFeature vocabulary. They distinguish outcomes such as a targeting match, percentage split, disabled or default decision, static value, offline use, and error.
References: modules/…/resolution_detail.go• L3-29 modules/…/evaluation.go• L55-66 modules/…/resolution_reason.go• L3-43
Typed results
model.VariationResult[T] represents a typed boolean, number, string, array, or object result. When evaluation cannot produce the requested type, it carries the SDK default instead. RawVarResult supports boundaries that need the value without a fixed Go type.
The result keeps its failure state and error code beside its value. This lets a caller preserve safe behavior without losing the signal that the decision failed. The result can also cross a JSON boundary when a runtime or API needs a serialized form.
References: modules/…/variation_result.go• L9-46 modules/…/evaluation.go• L37-66
Error conditions
Error codes classify failures at the evaluation boundary. They cover missing flags, invalid contexts, missing targeting keys, parse errors, type mismatches, provider readiness, general errors, and invalid flag configuration.
Internal errors preserve the difference between normal rule flow and a failed evaluation. A rule that does not match can be skipped. A malformed rule or a missing key needed for bucketing must instead remain visible as an evaluation or configuration error.
References: modules/…/error_code.go• L3-20 modules/…/targeting_not_apply.go• L9-15 modules/…/nested_key_not_found.go• L5-12 modules/…/empty_bucketing_key.go• L5-12 modules/…/internal_flag.go• L164-183
Conversion and utility boundary
Conversion and utility packages keep external data and shared evaluation rules aligned. They normalize flag and request data before evaluation, then provide common behavior for query input, nested attributes, value types, and stable assignment.
External flag conversion
The DTO is the configuration-facing flag shape. Its converter maps variations, rules, bucketing, rollout data, state, version, and metadata into the internal flag model. The reverse path supports integrations that need to return the core model to a configuration boundary.
This boundary lets different configuration sources use one evaluation model. It also makes conversion mapping part of the compatibility contract for flag data.
References: modules/…/dto.go• L7-60 modules/…/converter.go• L7-51
Request context conversion
Request data often comes from JSON. JSON decoders can represent whole numbers as floating-point values, so request conversion restores integral values to integer values when possible. This keeps query attributes consistent with contexts created directly in Go.
The resulting custom attribute map also supports nested paths. A flag can use those paths when it selects a custom bucketing key.
References: modules/…/evaluation_ctx_converter.go• L5-26 modules/…/nested_field.go• L9-30 modules/…/number_utils.go• L3-6
Shared evaluation utilities
The utility package holds small rules that can change an evaluation outcome. It identifies JSON queries, trims query input, checks variation types, resolves nested fields, and builds stable hash buckets.
These helpers are shared behavior rather than local convenience code. A change can affect rule selection, configuration validation, numeric attribute handling, or user assignment across every runtime that uses the core module.
References: modules/…/json_check.go• L3-9 modules/…/str_trim.go• L3-11 modules/…/json_type_extractor.go• L3-16 modules/…/hash.go• L5-18
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the flag, resolution detail, or variation result contracts can affect SDK methods, relay responses, WASM output, and event fields.
- Changes to context serialization or protected data extraction can break query attributes, controlled dates, bulk flag selection, or event metadata.
- Changes to bucketing inputs or hash behavior can move subjects between percentage and progressive rollout assignments.
- Changes to DTO fields or conversion mapping must remain compatible with configuration readers and flag cache paths.
- Changes to error codes or resolution reasons can affect consumers that treat these values as stable OpenFeature data.
- Changes to query-context reuse must preserve the same context view for Nikunjy and JSONLogic rules. A failed JSON conversion must not become a cached usable context.
- Changes to the core module version require checking the separately released module and the root SDK dependency in
go.mod. - Changes to the root Go version or dependency replacement rules can alter which released core module and rule parser the SDK builds with.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A percentage or progressive rule has no usable targeting or bucketing key | The flag returns the SDK default with a targeting-key error instead of assigning a bucket | Supply the required key or use a rule that does not need bucketing. |
| A custom bucketing path is missing or is not a string | Evaluation cannot create a stable bucket and returns an error | Keep the nested path present and string-valued in the context. |
| A flag has invalid variations, no valid default rule, or a missing referenced variation | Validation or evaluation returns a configuration error | Validate the flag before placing it in the evaluation cache. |
| A selected variation does not match the requested SDK type | The caller receives the SDK default with a TYPE_MISMATCH result | Use the variation method that matches the configured value type. |
| A query cannot be parsed or evaluated | The rule does not apply, so a later rule or the default rule may decide the flag | Validate query syntax and use context attribute types supported by the query. |
| Shared query context data is changed while rules evaluate | Later rules can see a different context and produce inconsistent decisions | Treat the prepared query context as read-only during one flag evaluation. |
| The root SDK and core module use incompatible released contracts | Callers can see build errors or changed evaluation behavior at the module boundary | Upgrade the root dependency with the separately versioned core module. |
| The root module uses a different Go or rule-parser dependency policy from the core module | Builds can resolve different language or rule behavior than the core release expects | Review go.mod and modules/core/go.mod together when changing module or parser dependencies. |
Key files
| File | Why to read it |
|---|---|
go.mod | Shows the root SDK dependency on the released core module and the root dependency policy. |
modules/core/go.mod | Shows the independent module boundary and its direct rule-parser dependency. |
| modules/…/internal_flag.go | Shows how flag state, context, targeting, rollouts, and resolution details form one decision. |
| modules/…/rule.go | Shows the supported targeting, percentage, and progressive rollout choices. It also defines the shared query view used across rules. |
| modules/…/evaluation.go | Shows how a flag decision becomes a typed result. |
| modules/…/context.go | Shows the identity and attributes supplied to evaluation. |
| modules/…/goff_context_specifics.go | Shows the protected control data shared with higher-level flows. |
| modules/…/variation_result.go | Defines the result contract used by SDK and runtime boundaries. |
| modules/…/resolution_detail.go | Defines the decision details carried from flag evaluation. |
| modules/…/error_code.go | Defines the error values used by callers and integrations. |
| modules/…/converter.go | Shows the boundary between external flag data and the core flag model. |
| modules/…/serializer.go | Shows the context shape supplied to targeting queries. |
| modules/…/query_context_internal_test.go | Shows the contract that query context maps and JSON data are reused without changing their meaning. |
| modules/…/rule_bench_test.go | Shows the performance-sensitive paths for repeated rule and multi-rule evaluation. |
| modules/…/CHANGELOG.md | Records the core module’s separate release and compatibility history, including the v0.8.0 query-context changes. |