github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/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/powerman/golang-tools/go/packages"
    11  	"github.com/powerman/golang-tools/internal/packagesinternal"
    12  	"github.com/powerman/golang-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/powerman/golang-tools/internal/lsp/cache [github.com/powerman/golang-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  // ModuleInfo implements the source.Metadata interface.
    60  func (m *Metadata) ModuleInfo() *packages.Module {
    61  	return m.Module
    62  }
    63  
    64  // KnownMetadata is a wrapper around metadata that tracks its validity.
    65  type KnownMetadata struct {
    66  	*Metadata
    67  
    68  	// Valid is true if the given metadata is Valid.
    69  	// Invalid metadata can still be used if a metadata reload fails.
    70  	Valid bool
    71  
    72  	// ShouldLoad is true if the given metadata should be reloaded.
    73  	ShouldLoad bool
    74  }