-
-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathsession_options.go
More file actions
202 lines (163 loc) · 5.17 KB
/
Copy pathsession_options.go
File metadata and controls
202 lines (163 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package ferret
import (
"fmt"
"io"
"strings"
"github.com/MontFerret/ferret/v2/pkg/debugger"
encodingjson "github.com/MontFerret/ferret/v2/pkg/encoding/json"
"github.com/MontFerret/ferret/v2/pkg/logging"
"github.com/MontFerret/ferret/v2/pkg/runtime"
"github.com/MontFerret/ferret/v2/pkg/vm"
)
type (
sessionOptions struct {
logger []logging.Option
outputContentType string
env []vm.EnvironmentOption
debugFormat debugger.FormatOptions
}
// SessionOption configures a Session created from a Plan.
SessionOption func(*sessionOptions) error
)
func newSessionOptions(setters []SessionOption) (*sessionOptions, error) {
opts := &sessionOptions{
outputContentType: encodingjson.ContentType,
debugFormat: debugger.DefaultFormatOptions(),
}
for _, setter := range setters {
if setter == nil {
continue
}
if err := setter(opts); err != nil {
return nil, err
}
}
return opts, nil
}
// WithDebugFormat configures bounded debugger value formatting.
func WithDebugFormat(options DebugFormatOptions) SessionOption {
return func(session *sessionOptions) error {
if options.MaxDepth <= 0 || options.MaxItems <= 0 || options.MaxBytes <= 0 {
return fmt.Errorf("debug format limits must be positive")
}
session.debugFormat = options
return nil
}
}
// WithEnvironmentOptions appends VM environment options to the created session.
func WithEnvironmentOptions(opts ...vm.EnvironmentOption) SessionOption {
return func(session *sessionOptions) error {
if session == nil {
return nil
}
if len(opts) == 0 {
return nil
}
for _, opt := range opts {
if opt == nil {
continue
}
session.env = append(session.env, opt)
}
return nil
}
}
// WithOutputContentType selects the output codec content type for session results.
func WithOutputContentType(contentType string) SessionOption {
return func(session *sessionOptions) error {
if session == nil {
return nil
}
trimmed := strings.TrimSpace(contentType)
if trimmed == "" {
return fmt.Errorf("output content type cannot be empty")
}
session.outputContentType = trimmed
return nil
}
}
// WithSessionParams merges the provided parameter map into the session environment,
// overriding existing keys while preserving any other previously defined parameters.
func WithSessionParams(params map[string]any) SessionOption {
return func(s *sessionOptions) error {
if len(params) == 0 {
return nil
}
rtp, err := runtime.NewParamsFrom(params)
if err != nil {
return fmt.Errorf("failed to convert params to runtime.Params: %w", err)
}
return WithEnvironmentOptions(vm.WithParams(rtp))(s)
}
}
// WithSessionRuntimeParams merges the provided runtime.Params into the session environment,
// overriding existing keys while preserving any other previously defined parameters.
func WithSessionRuntimeParams(params runtime.Params) SessionOption {
return func(s *sessionOptions) error {
if len(params) == 0 {
return nil
}
return WithEnvironmentOptions(vm.WithParams(params))(s)
}
}
// WithSessionParam adds or overrides a single session parameter.
func WithSessionParam(name string, value any) SessionOption {
return func(s *sessionOptions) error {
if name == "" {
return fmt.Errorf("param name cannot be empty")
}
if value == nil {
return fmt.Errorf("param value cannot be nil")
}
rtp, err := runtime.NewParamsFrom(map[string]any{name: value})
if err != nil {
return fmt.Errorf("failed to convert param to runtime.Params: %w", err)
}
return WithEnvironmentOptions(vm.WithParams(rtp))(s)
}
}
// WithSessionRuntimeParam adds or overrides a single session parameter using a pre-converted runtime.Value.
func WithSessionRuntimeParam(name string, value runtime.Value) SessionOption {
return func(s *sessionOptions) error {
if name == "" {
return fmt.Errorf("param name cannot be empty")
}
if value == nil {
return fmt.Errorf("param value cannot be nil")
}
return WithEnvironmentOptions(vm.WithParam(name, value))(s)
}
}
// WithSessionLog sets the writer for logging output.
// The writer can be any io.Writer, such as os.Stdout or a file.
func WithSessionLog(writer io.Writer) SessionOption {
return func(opts *sessionOptions) error {
if writer == nil {
return fmt.Errorf("log writer cannot be nil")
}
opts.logger = append(opts.logger, logging.WithWriter(writer))
return nil
}
}
// WithSessionLogLevel sets the logging level for the session.
// The logging level determines the severity of log messages that will be recorded.
func WithSessionLogLevel(lvl logging.LogLevel) SessionOption {
return func(opts *sessionOptions) error {
if lvl < logging.TraceLevel || lvl > logging.Disabled {
return fmt.Errorf("invalid log level: %v", lvl)
}
opts.logger = append(opts.logger, logging.WithLevel(lvl))
return nil
}
}
// WithSessionLogFields sets the fields to be included in log entries for the session.
// These fields can provide additional context for debugging and monitoring purposes.
func WithSessionLogFields(fields map[string]any) SessionOption {
return func(opts *sessionOptions) error {
if len(fields) == 0 {
return nil
}
opts.logger = append(opts.logger, logging.WithFields(fields))
return nil
}
}