1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package managedwriter
16
17import "google.golang.org/protobuf/types/descriptorpb"
18
19// WriterOption are variadic options used to configure a ManagedStream instance.
20type WriterOption func(*ManagedStream)
21
22// WithType sets the stream type for the managed stream.
23func WithType(st StreamType) WriterOption {
24	return func(ms *ManagedStream) {
25		ms.streamSettings.streamType = st
26	}
27}
28
29// WithStreamName allows users to set the stream name this writer will
30// append to explicitly.  By default, the managed client will create the
31// stream when instantiated if necessary.
32//
33// Note:  Supplying this option causes other options which affect stream construction
34// such as WithStreamType and WithDestinationTable to be ignored.
35func WithStreamName(name string) WriterOption {
36	return func(ms *ManagedStream) {
37		ms.streamSettings.streamID = name
38	}
39}
40
41// WithDestinationTable specifies the destination table to which a created
42// stream will append rows.  Format of the table:
43//
44//   projects/{projectid}/datasets/{dataset}/tables/{table}
45func WithDestinationTable(destTable string) WriterOption {
46	return func(ms *ManagedStream) {
47		ms.destinationTable = destTable
48	}
49}
50
51// WithMaxInflightRequests bounds the inflight appends on the write connection.
52func WithMaxInflightRequests(n int) WriterOption {
53	return func(ms *ManagedStream) {
54		ms.streamSettings.MaxInflightRequests = n
55	}
56}
57
58// WithMaxInflightBytes bounds the inflight append request bytes on the write connection.
59func WithMaxInflightBytes(n int) WriterOption {
60	return func(ms *ManagedStream) {
61		ms.streamSettings.MaxInflightBytes = n
62	}
63}
64
65// WithTraceID allows instruments requests to the service with a custom trace prefix.
66// This is generally for diagnostic purposes only.
67func WithTraceID(traceID string) WriterOption {
68	return func(ms *ManagedStream) {
69		ms.streamSettings.TraceID = traceID
70	}
71}
72
73// WithSchemaDescriptor describes the format of the serialized data being sent by
74// AppendRows calls on the stream.
75func WithSchemaDescriptor(dp *descriptorpb.DescriptorProto) WriterOption {
76	return func(ms *ManagedStream) {
77		ms.schemaDescriptor = dp
78	}
79}
80
81// WithDataOrigin is used to attach an origin context to the instrumentation metrics
82// emitted by the library.
83func WithDataOrigin(dataOrigin string) WriterOption {
84	return func(ms *ManagedStream) {
85		ms.streamSettings.dataOrigin = dataOrigin
86	}
87}
88