Skip to content

Commit bc4115b

Browse files
committed
Move GetExperiments to a common place for other boot.go
1 parent e4cd040 commit bc4115b

File tree

3 files changed

+82
-32
lines changed

3 files changed

+82
-32
lines changed

sdks/go/container/tools/pipeline_options.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,36 @@ func MakePipelineOptionsFileAndEnvVar(options string) error {
4242
os.Setenv("PIPELINE_OPTIONS_FILE", f.Name())
4343
return nil
4444
}
45+
46+
type PipelineOptionsData struct {
47+
Options OptionsData `json:"options"`
48+
}
49+
50+
type OptionsData struct {
51+
Experiments []string `json:"experiments"`
52+
}
53+
54+
// GetExperiments extracts a string array from the options string (in JSON format)
55+
//
56+
// The json string of pipeline options is in the following format.
57+
// We only focus on experiments here.
58+
//
59+
// {
60+
// "display_data": [
61+
// {...},
62+
// ],
63+
// "options": {
64+
// ...
65+
// "experiments": [
66+
// ...
67+
// ],
68+
// }
69+
// }
70+
func GetExperiments(options string) []string {
71+
var opts PipelineOptionsData
72+
err := json.Unmarshal([]byte(options), &opts)
73+
if err != nil {
74+
return nil
75+
}
76+
return opts.Options.Experiments
77+
}

sdks/go/container/tools/pipeline_options_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,51 @@ func TestMakePipelineOptionsFileAndEnvVar(t *testing.T) {
5656
}
5757
os.Remove("pipeline_options.json")
5858
}
59+
60+
func TestGetExperiments(t *testing.T) {
61+
tests := []struct {
62+
name string
63+
inputOptions string
64+
expectedExps []string
65+
}{
66+
{
67+
"no experiments",
68+
`{"options": {"a": "b"}}`,
69+
nil,
70+
},
71+
{
72+
"valid experiments",
73+
`{"options": {"experiments": ["a", "b"]}}`,
74+
[]string{"a", "b"},
75+
},
76+
{
77+
"empty experiments",
78+
`{"options": {"experiments": []}}`,
79+
[]string{},
80+
},
81+
{
82+
"invalid json",
83+
`{options: {"experiments": []}}`,
84+
nil,
85+
},
86+
{
87+
"empty string",
88+
"",
89+
nil,
90+
},
91+
}
92+
93+
for _, test := range tests {
94+
t.Run(test.name, func(t *testing.T) {
95+
exps := GetExperiments(test.inputOptions)
96+
if len(exps) != len(test.expectedExps) {
97+
t.Errorf("got: %v, want: %v", exps, test.expectedExps)
98+
}
99+
for i, v := range exps {
100+
if v != test.expectedExps[i] {
101+
t.Errorf("got: %v, want: %v", exps, test.expectedExps)
102+
}
103+
}
104+
})
105+
}
106+
}

sdks/python/container/boot.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,37 +117,6 @@ func main() {
117117
}
118118
}
119119

120-
// The json string of pipeline options is in the following format.
121-
// We only focus on experiments here.
122-
//
123-
// {
124-
// "display_data": [
125-
// {...},
126-
// ],
127-
// "options": {
128-
// ...
129-
// "experiments": [
130-
// ...
131-
// ],
132-
// }
133-
// }
134-
type PipelineOptionsData struct {
135-
Options OptionsData `json:"options"`
136-
}
137-
138-
type OptionsData struct {
139-
Experiments []string `json:"experiments"`
140-
}
141-
142-
func getExperiments(options string) []string {
143-
var opts PipelineOptionsData
144-
err := json.Unmarshal([]byte(options), &opts)
145-
if err != nil {
146-
return nil
147-
}
148-
return opts.Options.Experiments
149-
}
150-
151120
func launchSDKProcess() error {
152121
ctx := grpcx.WriteWorkerID(context.Background(), *id)
153122

@@ -187,7 +156,7 @@ func launchSDKProcess() error {
187156
logger.Fatalf(ctx, "Failed to convert pipeline options: %v", err)
188157
}
189158

190-
experiments := getExperiments(options)
159+
experiments := tools.GetExperiments(options)
191160
pipNoBuildIsolation = false
192161
if slices.Contains(experiments, "pip_no_build_isolation") {
193162
pipNoBuildIsolation = true

0 commit comments

Comments
 (0)