Skip to content

feat: Add GetFeatureByName and GetSegmentByName - #22

Closed
matthewelwell wants to merge 1 commit into
mainfrom
feat/lookup-features-and-segments-by-name
Closed

feat: Add GetFeatureByName and GetSegmentByName#22
matthewelwell wants to merge 1 commit into
mainfrom
feat/lookup-features-and-segments-by-name

Conversation

@matthewelwell

Copy link
Copy Markdown
Contributor

Adds lookups by name for features and segments, so callers that know a project UUID and a name — but not a UUID — can resolve an entity.

This unblocks terraform-provider-flagsmith#153, which asks for flagsmith_feature and flagsmith_segment data sources so that features created outside of Terraform can be referenced.

func (c *Client) GetFeatureByName(projectUUID, featureName string) (*Feature, error)
func (c *Client) GetSegmentByName(projectUUID, segmentName string) (*Segment, error)

Things worth knowing about the API

Both are built on a shared searchProjectResources helper, because two API details make this less trivial than it looks:

  • Features filter on search, segments filter on q. Passing the wrong param is silently ignored and returns every result in the project.
  • Both filters are case-insensitive contains matches, so results have to be filtered client-side for an exact match. Without that, asking for flag could resolve to flagship.

Pages are requested as page=N against the client's configured base URL rather than by following DRF's next link, which is built from the incoming request host and so can be unreachable behind a self-hosted reverse proxy. next is used only as a "there is more" signal.

ProjectUUID is set from the argument, since the API never returns it. The Terraform provider depends on this: a data source read whose required attributes don't echo the configuration is rejected by Terraform core.

Duplicate names

Segment names are not unique within a project (Segment.Meta has no constraint), so a name matching more than one segment returns MultipleSegmentsFoundError rather than picking arbitrarily. Feature names are uniquely indexed, so MultipleFeaturesFoundError is defensive only — but it fails loudly rather than silently choosing a row if that invariant ever changes.

Tests

19 new unit tests in search_test.go covering exact-match filtering, partial-match-only, case sensitivity, pagination, duplicate matches, API errors, an unresolvable project, the segment rules tree, feature-specific segments, and that the search/q params and ProjectUUID stamping are correct. No existing behaviour changes.

Release

Needs a v0.12.0 tag once merged, so the Terraform provider can depend on it.

🤖 Generated with Claude Code

@matthewelwell
matthewelwell requested a review from a team as a code owner August 25, 2026 16:39
@matthewelwell
matthewelwell requested review from khvn26 and removed request for a team August 25, 2026 16:39
@matthewelwell
matthewelwell marked this pull request as draft August 25, 2026 16:49
@matthewelwell
matthewelwell removed the request for review from khvn26 August 25, 2026 16:54
@matthewelwell
matthewelwell force-pushed the feat/lookup-features-and-segments-by-name branch from 0055c7c to 7cca891 Compare August 25, 2026 17:20
Adds lookups by name for features and segments, so that callers which know a
project UUID and a name -- but not a UUID -- can resolve an entity. This is
what the Terraform provider needs to offer `flagsmith_feature` and
`flagsmith_segment` data sources.

Two things about the API are worth knowing:

- Features filter on `search`, segments filter on `q`. Passing the wrong one is
  silently ignored and returns every result in the project.
- Both filters are case insensitive "contains" matches, so results have to be
  filtered client side for an exact match. Without that, asking for `flag`
  could return `flagship`.

Each lookup is a single request. CustomPagination allows at most 999 results per
page, which is sent explicitly so that a change to the API's default page size
cannot silently truncate a search. A truncated page means an exact match may
have been missed, so that returns an error rather than a false "not found"; it
takes a project with more than 999 entities whose names all contain the search
term to reach.

Segment names are not unique within a project, so a name that matches more than
one segment returns MultipleSegmentsFoundError rather than picking arbitrarily.
Feature names are uniquely indexed, so the equivalent feature error is
defensive only.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@matthewelwell
matthewelwell force-pushed the feat/lookup-features-and-segments-by-name branch from 7cca891 to b617e75 Compare August 25, 2026 17:32
@matthewelwell

Copy link
Copy Markdown
Contributor Author

Superseded: the Terraform provider data sources are now keyed on uuid (see terraform-provider-flagsmith#244), which GetFeature and GetSegment already cover on v0.11.1. No client change is needed for that issue after all.

Lookup by name is still a reasonable addition if it's wanted later — the branch is kept for reference. The two API details it dealt with are worth recording either way: features filter on search while segments filter on q, and both are case-insensitive contains matches, so exact matching has to happen client-side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant