open-feature / go-sdk

Go SDK for OpenFeature
https://openfeature.dev
Apache License 2.0
126 stars 30 forks source link

feat: isolate interfaces from SDK to improve testability #268

Closed Kavindu-Dodan closed 3 months ago

Kavindu-Dodan commented 4 months ago

This PR

Partially address #266 by providing interface contracts for the SDK. There are no breaking changes in this change

I have introduced IEvaluation interface, which represents the general OpenFeatuer API layer [1]. Before this change, there was no such interface so mocking API interactions was impossible. To obtain the singleton instance of IEvaluation interface, I have introduced GetApiInstance() helper.

Consider the example below,

// get the global instance that may be injected into testable components 
apiInstance := openfeature.GetApiInstance()

// use for OpenFeature operations 
apiInstance.SetProvider(myProvider)
apiInstance.AddHandler(....)

In the future, we can update our documentation to make API instance based operations the preferred interaction method.

Further, IEvaluation exposes client creation methods GetClient() & GetNamedClient(clientName string) (similar to spec [2]). These methods return IClient instances, making it easy to test OpenFeature client related operations.

apiInstance := openfeature.GetApiInstance()

// IClient instance, which can be mocked when injected into preferred locations 
client := apiInstance.GetClient()

[1] - https://openfeature.dev/specification/sections/flag-evaluation [2] - https://openfeature.dev/specification/sections/flag-evaluation#creating-clients

codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 91.66667% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 81.92%. Comparing base (aeb4f6c) to head (c7ede79).

Files Patch % Lines
openfeature/client.go 72.22% 5 Missing :warning:
openfeature/openfeature_api.go 93.33% 2 Missing and 2 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #268 +/- ## ========================================== + Coverage 81.74% 81.92% +0.17% ========================================== Files 10 10 Lines 964 979 +15 ========================================== + Hits 788 802 +14 Misses 156 156 - Partials 20 21 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

hairyhenderson commented 3 months ago

sorry for the slow response on this, and thanks for merging it!