Copyright 2020 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 godoc

import (
	
	
	
	
	
	

	
	
)
The encoding type identifies the encoding being used, to distinguish them when reading from the DB.
const (
	encodingTypeLen  = 4 // all encoding types must be this many bytes
	fastEncodingType = "AST2"
)
ErrInvalidEncodingType is returned when the data to DecodePackage has an invalid encoding type.
var ErrInvalidEncodingType = fmt.Errorf("want initial bytes to be %q but they aren't", fastEncodingType)
Encode encodes a Package into a byte slice. During its operation, Encode modifies the AST, but it restores it to a state suitable for rendering before it returns.
func ( *Package) ( context.Context) ( []byte,  error) {
	defer derrors.Wrap(&, "godoc.Package.Encode()")
	return .fastEncode()
}
DecodPackage decodes a byte slice encoded with Package.Encode into a Package.
func ( []byte) ( *Package,  error) {
	defer derrors.Wrap(&, "DecodePackage()")

	if len() < encodingTypeLen {
		return nil, ErrInvalidEncodingType
	}
	switch string([:encodingTypeLen]) {
	case fastEncodingType:
		return fastDecodePackage([encodingTypeLen:])
	default:
		return nil, ErrInvalidEncodingType
	}
}

func ( *Package) () ( []byte,  error) {
	defer derrors.Wrap(&, "godoc.Package.FastEncode()")

	var  bytes.Buffer
	io.WriteString(&, fastEncodingType)
	 := codec.NewEncoder()
	,  := fsetToBytes(.Fset)
	if  != nil {
		return nil, 
	}
	if  := .Encode();  != nil {
		return nil, 
	}
	if  := .Encode(&.encPackage);  != nil {
		return nil, 
	}
	.Write(.Bytes())
	return .Bytes(), nil
}

func ( []byte) ( *Package,  error) {
	defer derrors.Wrap(&, "FastDecodePackage()")

	 := codec.NewDecoder()
	,  := .Decode()
	if  != nil {
		return nil, 
	}
	,  := .([]byte)
	if ! {
		return nil, fmt.Errorf("first decoded value is %T, wanted []byte", )
	}
	,  := fsetFromBytes()
	if  != nil {
		return nil, 
	}
	,  = .Decode()
	if  != nil {
		return nil, 
	}
	,  := .(*encPackage)
	if ! {
		return nil, fmt.Errorf("second decoded value is %T, wanted *encPackage", )
	}
	return &Package{
		Fset:       ,
		encPackage: *,
	}, nil
}
token.FileSet uses some unexported types in its encoding, so we can't use our own codec from it. Instead we use gob and encode the resulting bytes.
func ( *token.FileSet) ([]byte, error) {
	var  bytes.Buffer
	 := gob.NewEncoder(&)
	if  := .Write(.Encode);  != nil {
		return nil, 
	}
	return .Bytes(), nil
}

func ( []byte) (*token.FileSet, error) {
	 := gob.NewDecoder(bytes.NewReader())
	 := token.NewFileSet()
	if  := .Read(.Decode);  != nil {
		return nil, 
	}
	return , nil
}
go:generate go run gen_ast.go
Used by the gen program to generate encodings for unexported types.