Copyright 2021 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package frontend

import (
	
	
	

	
	
)
Symbol is an element in the package API. A symbol can be a constant, variable, function, type, field or method.
Name is name of the symbol. At a given package version, name must be unique.
Synopsis is the one line description of the symbol that is displayed.
Section is the section that a symbol appears in.
Kind is the type of a symbol, which is either a constant, variable, function, type, field or method.
Link is the link to the symbol name on pkg.go.dev.
Children contain the child symbols for this symbol. This will only be populated when the SymbolType is "Type". For example, the children of net/http.Handler are FileServer, NotFoundHandler, RedirectHandler, StripPrefix, and TimeoutHandler. Each child symbol will have ParentName set to the Name of this type.
Builds lists all of the build contexts supported by the symbol, it is only available for limited set of builds. If the symbol supports all build contexts, Builds will be nil.
builds keeps track of build contexts used to generate Builds.
New indicates that the symbol is new as of the version where it is present. For example, if type Client was introduced in v1.0.0 and Client.Timeout was introduced in v1.1.0, New will be false for Client and true for Client.Timeout if this Symbol corresponds to v1.1.0.
	New bool
}

func ( *Symbol) ( ...internal.BuildContext) {
	if .builds == nil {
		.builds = map[internal.BuildContext]bool{}
	}
	for ,  := range  {
		.builds[] = true
	}
}
symbolsForVersions returns an array of symbols for use in the VersionSummary of the specified version.
func ( string,  map[string]map[internal.SymbolMeta]*internal.SymbolBuildContexts) [][]*Symbol {
	 := map[string]map[internal.SymbolMeta]*Symbol{}
	 := map[internal.SymbolMeta]*internal.SymbolBuildContexts{}
	for ,  := range  {
		for ,  := range  {
For the children, keep track of them for later.
				[] = 
				continue
			}

			,  := [.Name]
			if ! {
				 = map[internal.SymbolMeta]*Symbol{}
				[.Name] = 
			}
			,  := []
			if ! {
				 = &Symbol{
					Name:     .Name,
					Synopsis: .Synopsis,
					Section:  .Section,
					Kind:     .Kind,
					Link:     symbolLink(, .Name, .BuildContexts()),
					New:      true,
				}
				[.Name][] = 
			}
			.addBuilds(.BuildContexts()...)
		}
	}

For each child symbol, 1 of 3 things can occur: Option 1: If no parent exists for this child symbol, make one and add the parent to the map. Option 2: A parent exists and does not support the build context of the child. This occurs when the parent type is introduced for another build context, but was introduced at the previous version for the current child. Create a new parent for this child. Option 3: A parent exists and does support the build context of the child. Add the child to the parent.
		 := &Symbol{
			Name:     .Name,
			Synopsis: .Synopsis,
			Section:  .Section,
			Kind:     .Kind,
			Link:     symbolLink(, .Name, .BuildContexts()),
			New:      true,
		}

		 := findParent(.ParentName, , )
Option 3: found a relevant parent.
			.Children = append(.Children, )
			continue
		}
Option 1 and 2: We did not find a relevant parent, so create one. Since this parent is not introduced at this version, create a distinct type for each group of symbols. To do so, we make up a synopsis for the SymbolMeta below, since it is only used as a key in nameToMetaToSymbol. Example case: http://pkg.go.dev/internal/poll?tab=versions for go1.10 should show: type FD -- windows/amd64 FD.ReadMsg FD.WriteMsg type FD -- darwin/amd64, linux/amd64 FD.SetBlocking FD.WriteOnce
		 = createParent(.ParentName, , .BuildContexts()...)
		 := internal.SymbolMeta{
			Name:       .Name,
			ParentName: .Name,
			Synopsis:   fmt.Sprintf("type %s (%v)", .Name, .BuildContexts()),
			Section:    .Section,
			Kind:       .Kind,
		}
		.Children = append(.Children, )
		if ,  := [.Name]; ! {
			[.Name] = map[internal.SymbolMeta]*Symbol{}
		}
		[.Name][] = 
	}

	var  []*Symbol
	for ,  := range  {
		for ,  := range  {
			if len(.builds) != len(internal.BuildContexts) {
				for  := range .builds {
					.Builds = append(.Builds, fmt.Sprintf("%s/%s", .GOOS, .GOARCH))
				}
				sort.Strings(.Builds)
			}
			 = append(, )
		}
	}
	return sortSymbols()
}

func ( string,  *internal.SymbolBuildContexts,
	 map[string]map[internal.SymbolMeta]*Symbol) *Symbol {
	,  := []
	if ! {
		return nil
	}
	for ,  := range  {
		for  := range .builds {
			if .SupportsBuild() {
				return 
			}
		}
	}
	return nil
}

func (,  string,  []internal.BuildContext) string {
	if len() == len(internal.BuildContexts) {
		return fmt.Sprintf("%s#%s", , )
	}
When a symbol is introduced for a specific GOOS/GOARCH at a version, linking to an unspecified GOOS/GOARCH page might not take the user to the symbol. Instead, link to one of the supported build contexts.
	return fmt.Sprintf("%s?GOOS=%s#%s", , [0].GOOS, )
}
createParent creates a parent symbol for the provided unit symbol. This is used when us is a child of a symbol that may have been introduced at a different version. The symbol created will have New set to false, since this function is only used when a parent symbol is not found for the unit symbol, which means it was not introduced at the same version.
func (,  string,  ...internal.BuildContext) *Symbol {
	 := &Symbol{
		Name:     ,
		Synopsis: fmt.Sprintf("type %s", ),
		Section:  internal.SymbolSectionTypes,
		Kind:     internal.SymbolKindType,
		Link:     symbolLink(, , ),
	}
	.addBuilds(...)
	return 
}
sortSymbols returns an array of symbols in order of (1) Constants (2) Variables (3) Functions and (4) Types. Within each section, symbols are sorted alphabetically by name. In the types sections, aside from interfaces, child symbols are sorted in order of (1) Fields (2) Constants (3) Variables (4) Functions and (5) Methods. For interfaces, child symbols are sorted in order of (1) Methods (2) Constants (3) Variables and (4) Functions.
func ( []*Symbol) [][]*Symbol {
	 := map[internal.SymbolSection][]*Symbol{}
	for ,  := range  {
		[.Section] = append([.Section], )
		 := map[internal.SymbolKind][]*Symbol{}
		.Synopsis = strings.TrimSuffix(.Synopsis, "{ ... }")
		for ,  := range .Children {
			[.Kind] = append([.Kind], )
		}
		for ,  := range  {
			sortSymbolsGroup()
		}
		 := append(append(append(
			[internal.SymbolKindField],
			[internal.SymbolKindConstant]...),
			[internal.SymbolKindVariable]...),
			[internal.SymbolKindFunction]...)
		if strings.Contains(.Synopsis, "interface") {
			.Children = append([internal.SymbolKindMethod], ...)
		} else {
			.Children = append(, [internal.SymbolKindMethod]...)
		}
	}
	for ,  := range  {
		sortSymbolsGroup()
	}

	var  [][]*Symbol
	for ,  := range []internal.SymbolSection{
		internal.SymbolSectionConstants,
		internal.SymbolSectionVariables,
		internal.SymbolSectionFunctions,
		internal.SymbolSectionTypes} {
		if [] != nil {
			 = append(, [])
		}
	}
	return 
}

func ( []*Symbol) {
	sort.Slice(, func(,  int) bool {
		 := []
		 := []
		if .Synopsis != .Synopsis {
			return .Synopsis < .Synopsis
		}
		return compareStringSlices(.Builds, .Builds) < 0
	})
}

func (,  []string) int {
	for ,  := range  {
		if  >= len() { // first slice is longer, so greater
			return 1
		}
		if  := strings.Compare(, []);  != 0 {
			return 
		}
	}
	if len() == len() {
		return 0
first slice is shorter
	return -1
}
ParseVersionsDetails returns a map of versionToNameToUnitSymbol based on data from the proovided VersionDetails.
func ( VersionsDetails) ( *internal.SymbolHistory,  error) {
	 := internal.NewSymbolHistory()
	for ,  := range .ThisModule {
		for ,  := range .Versions {
			 := .Version
			if .ThisModule[0].ModulePath == stdlib.ModulePath {
				 = stdlib.VersionForTag()
			}
			for ,  := range .Symbols {
				for ,  := range  {
					if .New {
						addSymbol(, , , .Builds)
					}
					for ,  := range .Children {
						addSymbol(, , , .Builds)
					}
				}
			}
		}
	}
	return , nil
}

func ( *Symbol,  string,  *internal.SymbolHistory,  []string) {
	 := internal.SymbolMeta{
		Name: .Name,
	}
	if len() == 0 {
		.AddSymbol(, , internal.BuildContextAll)
		return
	}
	for ,  := range  {
		 := strings.SplitN(, "/", 2)
		var  internal.BuildContext
		switch [0] {
		case "linux":
			 = internal.BuildContextLinux
		case "darwin":
			 = internal.BuildContextDarwin
		case "windows":
			 = internal.BuildContextWindows
		case "js":
			 = internal.BuildContextJS
		}
		.AddSymbol(, , )
	}