github.com/lyraproj/hiera@v1.0.0-rc4/config/uri.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/lyraproj/dgo/dgo"
     8  	"github.com/lyraproj/dgo/tf"
     9  	"github.com/lyraproj/dgo/util"
    10  	"github.com/lyraproj/dgo/vf"
    11  	"github.com/lyraproj/hiera/api"
    12  )
    13  
    14  type uri struct {
    15  	original string
    16  	resolved string
    17  }
    18  
    19  var uriType = tf.NewNamed(
    20  	`hiera.uri`,
    21  	func(v dgo.Value) dgo.Value {
    22  		m := v.(dgo.Map)
    23  		return &uri{
    24  			original: m.Get(`original`).(dgo.String).GoString(),
    25  			resolved: m.Get(`resolved`).(dgo.String).GoString()}
    26  	},
    27  	func(v dgo.Value) dgo.Value {
    28  		p := v.(*uri)
    29  		return vf.Map(
    30  			`original`, p.original,
    31  			`resolved`, p.resolved)
    32  	},
    33  	reflect.TypeOf(&uri{}),
    34  	reflect.TypeOf((*api.Location)(nil)).Elem(),
    35  	nil)
    36  
    37  // NewURI returns a URI Location
    38  func NewURI(original string) api.Location {
    39  	return &uri{original: original}
    40  }
    41  
    42  func (u *uri) Type() dgo.Type {
    43  	return uriType
    44  }
    45  
    46  func (u *uri) HashCode() int32 {
    47  	return util.StringHash(u.original)
    48  }
    49  
    50  func (u *uri) Equals(value interface{}) bool {
    51  	ou, ok := value.(*uri)
    52  	if ok {
    53  		ok = *u == *ou
    54  	}
    55  	return ok
    56  }
    57  
    58  func (u *uri) Exists() bool {
    59  	return true
    60  }
    61  
    62  func (u *uri) Kind() api.LocationKind {
    63  	return api.LcURI
    64  }
    65  
    66  func (u *uri) String() string {
    67  	return fmt.Sprintf("uri{original:%s, resolved:%s", u.original, u.resolved)
    68  }
    69  
    70  func (u *uri) Original() string {
    71  	return u.original
    72  }
    73  
    74  func (u *uri) Resolve(ic api.Invocation, dataDir string) []api.Location {
    75  	r, _ := ic.InterpolateString(u.original, false)
    76  	return []api.Location{&uri{u.original, r.String()}}
    77  }
    78  
    79  func (u *uri) Resolved() string {
    80  	return u.resolved
    81  }