go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/api/logpb/utils.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 logpb
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"reflect"
    21  
    22  	"go.chromium.org/luci/logdog/common/types"
    23  )
    24  
    25  var (
    26  	// ErrNoContent indicates that a LogEntry has no data content.
    27  	ErrNoContent = errors.New("no content")
    28  )
    29  
    30  // Validate returns an error if the supplied LogStreamDescriptor is not complete
    31  // and valid.
    32  //
    33  // If prefix is true, the Prefix field will be validated; otherwise, it will
    34  // be ignored. This can be useful when attempting to validate a
    35  // LogStreamDescriptor before the application-assigned Prefix is known.
    36  func (d *LogStreamDescriptor) Validate(prefix bool) error {
    37  	if d == nil {
    38  		return errors.New("descriptor is nil")
    39  	}
    40  
    41  	if prefix {
    42  		if err := types.StreamName(d.Prefix).Validate(); err != nil {
    43  			return fmt.Errorf("invalid prefix: %s", err)
    44  		}
    45  	}
    46  	if err := types.StreamName(d.Name).Validate(); err != nil {
    47  		return fmt.Errorf("invalid name: %s", err)
    48  	}
    49  
    50  	switch d.StreamType {
    51  	case StreamType_TEXT, StreamType_BINARY, StreamType_DATAGRAM:
    52  		break
    53  
    54  	default:
    55  		return fmt.Errorf("invalid stream type: %v", d.StreamType)
    56  	}
    57  
    58  	if d.ContentType == "" {
    59  		return errors.New("missing content type")
    60  	}
    61  
    62  	if d.Timestamp == nil {
    63  		return errors.New("missing timestamp")
    64  	}
    65  	for k, v := range d.GetTags() {
    66  		if err := types.ValidateTag(k, v); err != nil {
    67  			return fmt.Errorf("invalid tag %q: %v", k, err)
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  // Equal tests if two LogStreamDescriptor instances have the same data.
    74  func (d *LogStreamDescriptor) Equal(o *LogStreamDescriptor) bool {
    75  	return reflect.DeepEqual(d, o)
    76  }
    77  
    78  // Path returns a types.StreamPath constructed from the LogStreamDesciptor's
    79  // Prefix and Name fields.
    80  func (d *LogStreamDescriptor) Path() types.StreamPath {
    81  	return types.StreamName(d.Prefix).Join(types.StreamName(d.Name))
    82  }
    83  
    84  // Validate checks a supplied LogEntry against its LogStreamDescriptor for
    85  // validity, returning an error if it is not valid.
    86  //
    87  // If the LogEntry is otherwise valid, but has no content, ErrNoContent will be
    88  // returned.
    89  func (e *LogEntry) Validate(d *LogStreamDescriptor) error {
    90  	if e == nil {
    91  		return errors.New("entry is nil")
    92  	}
    93  
    94  	// Check for content.
    95  	switch d.StreamType {
    96  	case StreamType_TEXT:
    97  		if t := e.GetText(); t == nil || len(t.Lines) == 0 {
    98  			return ErrNoContent
    99  		}
   100  
   101  	case StreamType_BINARY:
   102  		if b := e.GetBinary(); b == nil || len(b.Data) == 0 {
   103  			return ErrNoContent
   104  		}
   105  
   106  	case StreamType_DATAGRAM:
   107  		if d := e.GetDatagram(); d == nil {
   108  			return ErrNoContent
   109  		}
   110  	}
   111  	return nil
   112  }