vitess.io/vitess@v0.16.2/go/vt/logutil/proto3.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package logutil
    18  
    19  import (
    20  	"time"
    21  
    22  	logutilpb "vitess.io/vitess/go/vt/proto/logutil"
    23  	vttimepb "vitess.io/vitess/go/vt/proto/vttime"
    24  )
    25  
    26  // This file contains a few functions to help with proto3.
    27  
    28  // ProtoToTime converts a vttimepb.Time to a time.Time.
    29  // proto3 will eventually support timestamps, at which point we'll retire
    30  // this.
    31  //
    32  // A nil pointer is like the empty timestamp.
    33  func ProtoToTime(ts *vttimepb.Time) time.Time {
    34  	if ts == nil {
    35  		// treat nil like the empty Timestamp
    36  		return time.Time{}
    37  	}
    38  	return time.Unix(ts.Seconds, int64(ts.Nanoseconds)).UTC()
    39  }
    40  
    41  // TimeToProto converts the time.Time to a vttimepb.Time.
    42  func TimeToProto(t time.Time) *vttimepb.Time {
    43  	seconds := t.Unix()
    44  	nanos := int64(t.Sub(time.Unix(seconds, 0)))
    45  	return &vttimepb.Time{
    46  		Seconds:     seconds,
    47  		Nanoseconds: int32(nanos),
    48  	}
    49  }
    50  
    51  // EventStream is an interface used by RPC clients when the streaming
    52  // RPC returns a stream of log events.
    53  type EventStream interface {
    54  	// Recv returns the next event in the logs.
    55  	// If there are no more, it will return io.EOF.
    56  	Recv() (*logutilpb.Event, error)
    57  }