go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamproto/streamType.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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  //      http://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  
    15  package streamproto
    16  
    17  import (
    18  	"encoding/json"
    19  	"flag"
    20  
    21  	"go.chromium.org/luci/common/flag/flagenum"
    22  	"go.chromium.org/luci/logdog/api/logpb"
    23  	"go.chromium.org/luci/logdog/common/types"
    24  )
    25  
    26  // StreamType is a flag- and JSON-compatible wrapper around the StreamType
    27  // protobuf field.
    28  type StreamType logpb.StreamType
    29  
    30  // DefaultContentType returns the default ContentType for a given stream type.
    31  func (t StreamType) DefaultContentType() types.ContentType {
    32  	switch logpb.StreamType(t) {
    33  	case logpb.StreamType_TEXT:
    34  		return types.ContentTypeText
    35  
    36  	case logpb.StreamType_DATAGRAM:
    37  		return types.ContentTypeLogdogDatagram
    38  
    39  	case logpb.StreamType_BINARY:
    40  		fallthrough
    41  	default:
    42  		return types.ContentTypeBinary
    43  	}
    44  }
    45  
    46  var _ interface {
    47  	json.Marshaler
    48  	json.Unmarshaler
    49  	flag.Value
    50  } = (*StreamType)(nil)
    51  
    52  var (
    53  	// StreamTypeFlagEnum maps configuration strings to their underlying StreamTypes.
    54  	StreamTypeFlagEnum = flagenum.Enum{
    55  		"text":     StreamType(logpb.StreamType_TEXT),
    56  		"binary":   StreamType(logpb.StreamType_BINARY),
    57  		"datagram": StreamType(logpb.StreamType_DATAGRAM),
    58  	}
    59  )
    60  
    61  // Set implements flag.Value.
    62  func (t *StreamType) Set(v string) error {
    63  	return StreamTypeFlagEnum.FlagSet(t, v)
    64  }
    65  
    66  // String implements flag.Value.
    67  func (t *StreamType) String() string {
    68  	return StreamTypeFlagEnum.FlagString(t)
    69  }
    70  
    71  // UnmarshalJSON implements json.Unmarshaler.
    72  func (t *StreamType) UnmarshalJSON(data []byte) error {
    73  	return StreamTypeFlagEnum.JSONUnmarshal(t, data)
    74  }
    75  
    76  // MarshalJSON implements json.Marshaler.
    77  func (t StreamType) MarshalJSON() ([]byte, error) {
    78  	return StreamTypeFlagEnum.JSONMarshal(t)
    79  }