kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/kzip/buildmetadata/buildmetadata.go (about)

     1  /*
     2   * Copyright 2020 The Kythe Authors. All rights reserved.
     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 buildmetadata provides utilities for working with metadata kzips.
    18  package buildmetadata // import "kythe.io/kythe/go/platform/kzip/buildmetadata"
    19  
    20  import (
    21  	"fmt"
    22  	"time"
    23  
    24  	kptypes "kythe.io/kythe/go/util/ptypes"
    25  
    26  	"github.com/golang/protobuf/ptypes"
    27  
    28  	apb "kythe.io/kythe/proto/analysis_go_proto"
    29  	spb "kythe.io/kythe/proto/storage_go_proto"
    30  )
    31  
    32  const (
    33  	// Language is the language recorded in the VName for BuildMetadata
    34  	// compilation units.
    35  	Language = "kythe_build_metadata"
    36  )
    37  
    38  // CreateMetadataUnit creates a compilation unit containing a BuildMetadata with
    39  // the given timestamp.
    40  func CreateMetadataUnit(corpus string, timestamp time.Time) (*apb.CompilationUnit, error) {
    41  	tp, err := ptypes.TimestampProto(timestamp)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("marshaling timestamp: %v", err)
    44  	}
    45  	meta := &apb.BuildMetadata{
    46  		CommitTimestamp: tp,
    47  	}
    48  	det, err := kptypes.MarshalAny(meta)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("marshaling details: %v", err)
    51  	}
    52  	return &apb.CompilationUnit{
    53  		VName: &spb.VName{
    54  			Language: Language,
    55  			Corpus:   corpus,
    56  		},
    57  		Details: []*kptypes.Any{det},
    58  	}, nil
    59  }