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

     1  /*
     2   * Copyright 2014 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 analysis defines interfaces used to locate and analyze compilation
    18  // units and their inputs.
    19  //
    20  // The CompilationAnalyzer interface represents a generic analysis process that
    21  // processes kythe.proto.CompilationUnit messages and their associated inputs.
    22  // The Fetcher interface expresses the ability to fetch required files from
    23  // storage based on their corpus-relative paths and digests.
    24  package analysis // import "kythe.io/kythe/go/platform/analysis"
    25  
    26  import (
    27  	"context"
    28  	"fmt"
    29  
    30  	"google.golang.org/protobuf/proto"
    31  
    32  	apb "kythe.io/kythe/proto/analysis_go_proto"
    33  	spb "kythe.io/kythe/proto/storage_go_proto"
    34  )
    35  
    36  // OutputFunc handles a single AnalysisOutput.
    37  type OutputFunc func(context.Context, *apb.AnalysisOutput) error
    38  
    39  // A CompilationAnalyzer processes compilation units and delivers output
    40  // artifacts to a user-supplied callback function.
    41  type CompilationAnalyzer interface {
    42  	// Analyze calls f on each analysis output resulting from the analysis of the
    43  	// given apb.CompilationUnit.  If f returns an error, f is no longer called
    44  	// and Analyze returns with the same error.
    45  	Analyze(ctx context.Context, req *apb.AnalysisRequest, f OutputFunc) (*apb.AnalysisResult, error)
    46  }
    47  
    48  // EntryOutput returns an OutputFunc that unmarshals each output's value as an
    49  // Entry and calls f on it.
    50  func EntryOutput(f func(context.Context, *spb.Entry) error) OutputFunc {
    51  	return func(ctx context.Context, out *apb.AnalysisOutput) error {
    52  		var entry spb.Entry
    53  		if err := proto.Unmarshal(out.Value, &entry); err != nil {
    54  			return fmt.Errorf("error unmarshaling Entry from AnalysisOutput: %v", err)
    55  		}
    56  		return f(ctx, &entry)
    57  	}
    58  }
    59  
    60  // A Fetcher provides the ability to fetch file contents from storage.
    61  type Fetcher interface {
    62  	// Fetch retrieves the contents of a single file.  At least one of path and
    63  	// digest must be provided; both are preferred.  The implementation may
    64  	// return an error if both are not set.
    65  	Fetch(path, digest string) ([]byte, error)
    66  }