Add an attribute reader interface and a config reader implementation.
This commit is contained in:
parent
7063ee1af2
commit
15fb969f3e
|
@ -0,0 +1,17 @@
|
|||
package consul
|
||||
|
||||
import "time"
|
||||
|
||||
type _AttrReader interface {
|
||||
GetBool(_SchemaAttr) bool
|
||||
GetBoolOK(_SchemaAttr) (b, ok bool)
|
||||
GetDurationOK(_SchemaAttr) (time.Duration, bool)
|
||||
GetFloat64OK(_SchemaAttr) (float64, bool)
|
||||
GetIntOK(_SchemaAttr) (int, bool)
|
||||
GetIntPtr(_SchemaAttr) *int
|
||||
GetString(_SchemaAttr) string
|
||||
GetStringOK(_SchemaAttr) (string, bool)
|
||||
GetStringPtr(_SchemaAttr) *string
|
||||
GetStringSlice(attrName _SchemaAttr) []string
|
||||
BackingType() string
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
type _ConfigReader struct {
|
||||
d *schema.ResourceData
|
||||
}
|
||||
|
||||
func _NewConfigReader(d *schema.ResourceData) *_ConfigReader {
|
||||
return &_ConfigReader{
|
||||
d: d,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) BackingType() string {
|
||||
return "config"
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetBool(attrName _SchemaAttr) bool {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(bool)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetBoolOK(attrName _SchemaAttr) (b, ok bool) {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(bool), true
|
||||
}
|
||||
|
||||
return false, false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetDurationOK(attrName _SchemaAttr) (time.Duration, bool) {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
d, err := time.ParseDuration(v.(string))
|
||||
if err != nil {
|
||||
return time.Duration(0), false
|
||||
}
|
||||
return d, true
|
||||
}
|
||||
|
||||
return time.Duration(0), false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetFloat64OK(attrName _SchemaAttr) (float64, bool) {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(float64), true
|
||||
}
|
||||
|
||||
return 0.0, false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetIntOK(attrName _SchemaAttr) (int, bool) {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(int), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetIntPtr(attrName _SchemaAttr) *int {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
i := v.(int)
|
||||
return &i
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetString(attrName _SchemaAttr) string {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(string)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetStringOK(attrName _SchemaAttr) (string, bool) {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return v.(string), true
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetStringPtr(attrName _SchemaAttr) *string {
|
||||
if v, ok := r.d.GetOk(string(attrName)); ok {
|
||||
switch v.(type) {
|
||||
case string:
|
||||
s := v.(string)
|
||||
return &s
|
||||
case *string:
|
||||
return v.(*string)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *_ConfigReader) GetStringSlice(attrName _SchemaAttr) []string {
|
||||
if listRaw, ok := r.d.GetOk(string(attrName)); ok {
|
||||
return listRaw.([]string)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue