github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/cache/metadata.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  import (
     8  	"go/types"
     9  
    10  	"github.com/jhump/golang-x-tools/go/packages"
    11  	"github.com/jhump/golang-x-tools/internal/packagesinternal"
    12  	"github.com/jhump/golang-x-tools/internal/span"
    13  )
    14  
    15  // Declare explicit types for package paths, names, and IDs to ensure that we
    16  // never use an ID where a path belongs, and vice versa. If we confused these,
    17  // it would result in confusing errors because package IDs often look like
    18  // package paths.
    19  type (
    20  	PackageID   string
    21  	PackagePath string
    22  	PackageName string
    23  )
    24  
    25  // Metadata holds package Metadata extracted from a call to packages.Load.
    26  type Metadata struct {
    27  	ID              PackageID
    28  	PkgPath         PackagePath
    29  	Name            PackageName
    30  	GoFiles         []span.URI
    31  	CompiledGoFiles []span.URI
    32  	ForTest         PackagePath
    33  	TypesSizes      types.Sizes
    34  	Errors          []packages.Error
    35  	Deps            []PackageID
    36  	MissingDeps     map[PackagePath]struct{}
    37  	Module          *packages.Module
    38  	depsErrors      []*packagesinternal.PackageError
    39  
    40  	// Config is the *packages.Config associated with the loaded package.
    41  	Config *packages.Config
    42  
    43  	// IsIntermediateTestVariant reports whether the given package is an
    44  	// intermediate test variant, e.g.
    45  	// "github.com/jhump/golang-x-tools/internal/lsp/cache [golang.org/x/tools/internal/lsp/source.test]".
    46  	IsIntermediateTestVariant bool
    47  }
    48  
    49  // Name implements the source.Metadata interface.
    50  func (m *Metadata) PackageName() string {
    51  	return string(m.Name)
    52  }
    53  
    54  // PkgPath implements the source.Metadata interface.
    55  func (m *Metadata) PackagePath() string {
    56  	return string(m.PkgPath)
    57  }
    58  
    59  // KnownMetadata is a wrapper around metadata that tracks its validity.
    60  type KnownMetadata struct {
    61  	*Metadata
    62  
    63  	// Valid is true if the given metadata is Valid.
    64  	// Invalid metadata can still be used if a metadata reload fails.
    65  	Valid bool
    66  
    67  	// ShouldLoad is true if the given metadata should be reloaded.
    68  	ShouldLoad bool
    69  }