gopkg.in/juju/charm.v6-unstable@v6.0.0-20171026192109-50d0c219b496/resource/type.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package resource
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  )
     9  
    10  // These are the valid resource types (except for unknown).
    11  const (
    12  	typeUnknown Type = iota
    13  	TypeFile
    14  )
    15  
    16  var types = map[Type]string{
    17  	TypeFile: "file",
    18  }
    19  
    20  // Type enumerates the recognized resource types.
    21  type Type int
    22  
    23  // ParseType converts a string to a Type. If the given value does not
    24  // match a recognized type then an error is returned.
    25  func ParseType(value string) (Type, error) {
    26  	for rt, str := range types {
    27  		if value == str {
    28  			return rt, nil
    29  		}
    30  	}
    31  	return typeUnknown, errors.Errorf("unsupported resource type %q", value)
    32  }
    33  
    34  // String returns the printable representation of the type.
    35  func (rt Type) String() string {
    36  	return types[rt]
    37  }
    38  
    39  // Validate ensures that the type is valid.
    40  func (rt Type) Validate() error {
    41  	// Ideally, only the (unavoidable) zero value would be invalid.
    42  	// However, typedef'ing int means that the use of int literals
    43  	// could result in invalid Type values other than the zero value.
    44  	if _, ok := types[rt]; !ok {
    45  		return errors.NewNotValid(nil, "unknown resource type")
    46  	}
    47  	return nil
    48  }