github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/parser/http.go (about)

     1  package parser
     2  
     3  import (
     4  	"encoding/json"
     5  	"go/token"
     6  	"os"
     7  )
     8  
     9  var _ Decl = HTTPDecl{}
    10  
    11  type HTTPDecl struct {
    12  	file  *File
    13  	pos   token.Position
    14  	value string
    15  }
    16  
    17  func (d HTTPDecl) String() string {
    18  	b, _ := json.Marshal(d)
    19  	return string(b)
    20  }
    21  
    22  func (d HTTPDecl) MarshalJSON() ([]byte, error) {
    23  	return json.Marshal(map[string]interface{}{
    24  		"type":  "pkger.Dir",
    25  		"file":  d.file,
    26  		"pos":   d.pos,
    27  		"value": d.value,
    28  	})
    29  }
    30  
    31  func (d HTTPDecl) File() (*File, error) {
    32  	if d.file == nil {
    33  		return nil, os.ErrNotExist
    34  	}
    35  	return d.file, nil
    36  }
    37  
    38  func (d HTTPDecl) Position() (token.Position, error) {
    39  	return d.pos, nil
    40  }
    41  
    42  func (d HTTPDecl) Value() (string, error) {
    43  	if d.value == "" {
    44  		return "", os.ErrNotExist
    45  	}
    46  	return d.value, nil
    47  }
    48  
    49  func (d HTTPDecl) Files(virtual map[string]string) ([]*File, error) {
    50  	od := OpenDecl{
    51  		file:  d.file,
    52  		pos:   d.pos,
    53  		value: d.value,
    54  	}
    55  
    56  	return od.Files(virtual)
    57  }