...

Source file src/github.com/launchdarkly/go-server-sdk/v6/testhelpers/ldservices/server_sdk_data.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/testhelpers/ldservices

     1  package ldservices
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
     8  	"github.com/launchdarkly/go-test-helpers/v3/httphelpers"
     9  	"github.com/launchdarkly/go-test-helpers/v3/jsonhelpers"
    10  )
    11  
    12  type fakeFlagOrSegment struct {
    13  	Key     string `json:"key"`
    14  	Version int    `json:"version"`
    15  }
    16  
    17  // FlagOrSegment provides a simple object that has only "key" and "version" properties.
    18  // This may be enough for some testing purposes that don't require full flag or segment data.
    19  func FlagOrSegment(key string, version int) interface{} {
    20  	return fakeFlagOrSegment{Key: key, Version: version}
    21  }
    22  
    23  // ServerSDKData is a convenience type for constructing a test server-side SDK data payload for PollingServiceHandler
    24  // or StreamingServiceHandler. Its String() method returns a JSON object with the expected "flags" and "segments"
    25  // properties.
    26  //
    27  //	data := NewServerSDKData().Flags(flag1, flag2)
    28  //	handler := PollingServiceHandler(data)
    29  type ServerSDKData struct {
    30  	FlagsMap    map[string]interface{} `json:"flags"`
    31  	SegmentsMap map[string]interface{} `json:"segments"`
    32  }
    33  
    34  // NewServerSDKData creates a ServerSDKData instance.
    35  func NewServerSDKData() *ServerSDKData {
    36  	return &ServerSDKData{make(map[string]interface{}), make(map[string]interface{})}
    37  }
    38  
    39  // String returns the JSON encoding of the struct as a string.
    40  func (s *ServerSDKData) String() string {
    41  	bytes, _ := json.Marshal(*s)
    42  	return string(bytes)
    43  }
    44  
    45  // Flags adds the specified items to the struct's "flags" map.
    46  //
    47  // Each item may be either a object produced by FlagOrSegment or a real data model object from the ldmodel
    48  // package. The minimum requirement is that when converted to JSON, it has a "key" property.
    49  func (s *ServerSDKData) Flags(flags ...interface{}) *ServerSDKData {
    50  	for _, flag := range flags {
    51  		if key := getKeyFromJSON(flag); key != "" {
    52  			s.FlagsMap[key] = flag
    53  		}
    54  	}
    55  	return s
    56  }
    57  
    58  // Segments adds the specified items to the struct's "segments" map.
    59  //
    60  // Each item may be either a object produced by FlagOrSegment or a real data model object from the ldmodel
    61  // package. The minimum requirement is that when converted to JSON, it has a "key" property.
    62  func (s *ServerSDKData) Segments(segments ...interface{}) *ServerSDKData {
    63  	for _, segment := range segments {
    64  		if key := getKeyFromJSON(segment); key != "" {
    65  			s.SegmentsMap[key] = segment
    66  		}
    67  	}
    68  	return s
    69  }
    70  
    71  func getKeyFromJSON(item interface{}) string {
    72  	return ldvalue.Parse(jsonhelpers.ToJSON(item)).GetByKey("key").StringValue()
    73  }
    74  
    75  // ToPutEvent creates an SSE event in the format that is used by the server-side SDK streaming endpoint.
    76  func (s *ServerSDKData) ToPutEvent() httphelpers.SSEEvent {
    77  	return httphelpers.SSEEvent{
    78  		Event: "put",
    79  		Data:  fmt.Sprintf(`{"path": "/", "data": %s}`, s),
    80  	}
    81  }
    82  

View as plain text