Skip to content

Commit 39d3851

Browse files
author
yanming
committed
Release 0.2.0
1 parent e6c70c2 commit 39d3851

File tree

11 files changed

+754
-145
lines changed

11 files changed

+754
-145
lines changed

.gitignore

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
# --------
22
# Golang
3-
# Date: 2019-05-23
43
# --------
54
# IntelliJ
65
*.iml
76
.idea/*
7+
/.vscode/
88
go.mod
9-
go.sum
9+
go.sum
10+
.DS_Store
11+
12+
13+
# --------
14+
# CLAUDE
15+
# --------
16+
CLAUDE.md
17+
18+
# --------
19+
# GEMINI
20+
# --------
21+
GEMINI.md

LICENSE

Lines changed: 0 additions & 28 deletions
This file was deleted.

beans/experiment.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package beans
22

3+
import (
4+
"encoding/json"
5+
)
6+
37
type Experiment struct {
48
// distinct_id 标识
59
DistinctId string
610
// 是否是登录 id
711
IsLoginId bool
12+
// 自定义主体 ID
13+
CustomIDs map[string]string
814
// 试验变量值
915
Result interface{}
1016
InternalExperiment InnerExperiment
1117
}
1218

19+
// 在代码中有一些浅拷贝操作,新增字段时需要注意
1320
type InnerExperiment struct {
1421
// 试验 ID
1522
AbtestExperimentId string `json:"abtest_experiment_id"`
@@ -79,3 +86,192 @@ type Variables struct {
7986
// 变量类型
8087
Type string `json:"type"`
8188
}
89+
90+
// AllExperimentsResult represents the result of fetching all experiments.
91+
// Its fields are unexported to ensure immutability and it should be created via the builder.
92+
type AllExperimentsResult struct {
93+
// distinct_id 标识
94+
distinctId string
95+
96+
// 是否是登录 id
97+
isLoginId bool
98+
99+
// 自定义主体 ID
100+
customIDs map[string]string
101+
102+
// 埋点回调函数,在fetchAll的时候自动生成,GetValue 时会自动调用
103+
trackCallback func(paramName string, experiment InnerExperiment)
104+
105+
// 全部的试验结果,用于支持 GetValue 方法
106+
experiments map[string]InnerExperiment
107+
108+
// 原始网络响应 body,用于跨服务传递
109+
responseBody string
110+
111+
// 请求时间
112+
timestamp int64
113+
}
114+
115+
// DistinctId returns the distinct_id.
116+
func (result *AllExperimentsResult) DistinctId() string {
117+
return result.distinctId
118+
}
119+
120+
// IsLoginId returns whether the id is a login id.
121+
func (result *AllExperimentsResult) IsLoginId() bool {
122+
return result.isLoginId
123+
}
124+
125+
// CustomIDs returns the custom IDs.
126+
func (result *AllExperimentsResult) CustomIDs() map[string]string {
127+
// 返回一个副本以防止外部修改 map
128+
if result.customIDs == nil {
129+
return nil
130+
}
131+
copiedCustomIDs := make(map[string]string, len(result.customIDs))
132+
for k, v := range result.customIDs {
133+
copiedCustomIDs[k] = v
134+
}
135+
return copiedCustomIDs
136+
}
137+
138+
// Timestamp returns the timestamp.
139+
func (result *AllExperimentsResult) Timestamp() int64 {
140+
return result.timestamp
141+
}
142+
143+
func (result *AllExperimentsResult) SetTrackCallback(callback func(paramName string, experiment InnerExperiment)) {
144+
result.trackCallback = callback
145+
}
146+
147+
// 安全的取值方法,自动埋点
148+
func (result *AllExperimentsResult) GetValue(paramName string, defaultValue interface{}) interface{} {
149+
var res interface{}
150+
var hitExperiment InnerExperiment
151+
if experiment, exists := result.experiments[paramName]; exists {
152+
res = experiment.Result
153+
hitExperiment = experiment
154+
} else {
155+
res = defaultValue
156+
}
157+
// 如果开启了埋点,则进行埋点
158+
if result.trackCallback != nil {
159+
result.trackCallback(paramName, hitExperiment)
160+
}
161+
return res
162+
}
163+
164+
// 获取 experiment对象,不会自动埋点,主要用于获取手动埋点的参数
165+
func (result *AllExperimentsResult) GetExperiment(paramName string, defaultValue interface{}) Experiment {
166+
if experiment, exists := result.experiments[paramName]; exists {
167+
return Experiment{
168+
DistinctId: result.DistinctId(),
169+
IsLoginId: result.IsLoginId(),
170+
CustomIDs: result.CustomIDs(),
171+
Result: experiment.Result,
172+
InternalExperiment: experiment,
173+
}
174+
175+
}
176+
return Experiment{
177+
Result: defaultValue,
178+
}
179+
}
180+
181+
// 检查是否包含指定参数
182+
func (result *AllExperimentsResult) HasParam(paramName string) bool {
183+
_, exists := result.experiments[paramName]
184+
return exists
185+
}
186+
187+
// DumpData 用于跨服务传递的序列化数据结构
188+
type DumpData struct {
189+
DistinctId string `json:"distinct_id"`
190+
IsLoginId bool `json:"is_login_id"`
191+
CustomIDs map[string]string `json:"custom_ids,omitempty"`
192+
ResponseBody string `json:"response_body"`
193+
Timestamp int64 `json:"timestamp"`
194+
}
195+
196+
// Dump 序列化完整的上下文信息(包括 distinct_id、is_login_id、custom_ids 和响应体)
197+
// 返回 JSON 字符串,用于跨服务传递
198+
func (result *AllExperimentsResult) Dump() (string, error) {
199+
data := DumpData{
200+
DistinctId: result.distinctId,
201+
IsLoginId: result.isLoginId,
202+
CustomIDs: result.customIDs,
203+
ResponseBody: result.responseBody,
204+
Timestamp: result.timestamp,
205+
}
206+
207+
jsonBytes, err := json.Marshal(data)
208+
if err != nil {
209+
return "", err
210+
}
211+
212+
return string(jsonBytes), nil
213+
}
214+
215+
// AllExperimentsResultBuilder is used to construct an AllExperimentsResult object.
216+
type AllExperimentsResultBuilder struct {
217+
distinctId string
218+
isLoginId bool
219+
customIDs map[string]string
220+
trackCallback func(paramName string, experiment InnerExperiment)
221+
experiments map[string]InnerExperiment
222+
responseBody string
223+
timestamp int64
224+
}
225+
226+
// NewAllExperimentsResultBuilder creates a new builder.
227+
func NewAllExperimentsResultBuilder() *AllExperimentsResultBuilder {
228+
return &AllExperimentsResultBuilder{}
229+
}
230+
231+
func (b *AllExperimentsResultBuilder) DistinctId(distinctId string) *AllExperimentsResultBuilder {
232+
b.distinctId = distinctId
233+
return b
234+
}
235+
236+
func (b *AllExperimentsResultBuilder) IsLoginId(isLoginId bool) *AllExperimentsResultBuilder {
237+
b.isLoginId = isLoginId
238+
return b
239+
}
240+
241+
func (b *AllExperimentsResultBuilder) CustomIDs(customIDs map[string]string) *AllExperimentsResultBuilder {
242+
b.customIDs = customIDs
243+
return b
244+
}
245+
246+
func (b *AllExperimentsResultBuilder) TrackCallback(callback func(paramName string, experiment InnerExperiment)) *AllExperimentsResultBuilder {
247+
b.trackCallback = callback
248+
return b
249+
}
250+
251+
func (b *AllExperimentsResultBuilder) Experiments(experiments map[string]InnerExperiment) *AllExperimentsResultBuilder {
252+
b.experiments = experiments
253+
return b
254+
}
255+
256+
func (b *AllExperimentsResultBuilder) ResponseBody(responseBody string) *AllExperimentsResultBuilder {
257+
b.responseBody = responseBody
258+
return b
259+
}
260+
261+
func (b *AllExperimentsResultBuilder) Timestamp(timestamp int64) *AllExperimentsResultBuilder {
262+
b.timestamp = timestamp
263+
return b
264+
}
265+
266+
// Build creates and returns the immutable AllExperimentsResult.
267+
func (b *AllExperimentsResultBuilder) Build() AllExperimentsResult {
268+
return AllExperimentsResult{
269+
distinctId: b.distinctId,
270+
isLoginId: b.isLoginId,
271+
customIDs: b.customIDs,
272+
trackCallback: b.trackCallback,
273+
experiments: b.experiments,
274+
responseBody: b.responseBody,
275+
timestamp: b.timestamp,
276+
}
277+
}

beans/request_params.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,35 @@ type RequestParam struct {
1111
Properties map[string]interface{}
1212

1313
// 自定义分流主体
14-
CustomIDs map[string]interface{}
14+
CustomIDs map[string]string
1515

1616
// 网络请求超时时间,单位 ms,默认 3s
1717
TimeoutMilliseconds int
1818

1919
// 是否自动采集 A/B Testing 埋点事件
2020
EnableAutoTrackABEvent bool
2121
}
22+
23+
// GetAll 接口的请求参数
24+
type FetchAllRequestParam struct {
25+
// HTTP 请求参数
26+
Properties map[string]interface{}
27+
28+
// 自定义分流主体
29+
CustomIDs map[string]string
30+
31+
// 网络请求超时时间,单位 ms,默认 3s
32+
TimeoutMilliseconds int
33+
34+
// 是否自动采集 A/B Testing 埋点事件
35+
EnableAutoTrackABEvent bool
36+
}
37+
38+
// LoadDumpedParam 是 LoadAllExperimentsFromSerialized 接口的请求参数
39+
type LoadDumpedParam struct {
40+
// 自定义分流主体
41+
CustomIDs map[string]string
42+
43+
// 是否自动采集 A/B Testing 埋点事件
44+
EnableAutoTrackABEvent bool
45+
}

0 commit comments

Comments
 (0)