本文整理匯總了Golang中github.com/mitchellh/mapstructure.WeakDecode函數的典型用法代碼示例。如果您正苦於以下問題:Golang WeakDecode函數的具體用法?Golang WeakDecode怎麽用?Golang WeakDecode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了WeakDecode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: inferTypeFromDefault
// inferTypeFromDefault contains the logic for the old method of inferring
// variable types - we can also use this for validating that the declared
// type matches the type of the default value
func (v *Variable) inferTypeFromDefault() VariableType {
if v.Default == nil {
return VariableTypeString
}
var s string
if err := mapstructure.WeakDecode(v.Default, &s); err == nil {
v.Default = s
return VariableTypeString
}
var m map[string]string
if err := mapstructure.WeakDecode(v.Default, &m); err == nil {
v.Default = m
return VariableTypeMap
}
var l []string
if err := mapstructure.WeakDecode(v.Default, &l); err == nil {
v.Default = l
return VariableTypeList
}
return VariableTypeUnknown
}
示例2: parseResources
func parseResources(result *structs.Resources, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) == 0 {
return nil
}
if len(list.Items) > 1 {
return fmt.Errorf("only one 'resource' block allowed per task")
}
// Get our resource object
o := list.Items[0]
// We need this later
var listVal *ast.ObjectList
if ot, ok := o.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return fmt.Errorf("resource: should be an object")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return err
}
delete(m, "network")
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
// Parse the network resources
if o := listVal.Filter("network"); len(o.Items) > 0 {
if len(o.Items) > 1 {
return fmt.Errorf("only one 'network' resource allowed")
}
var r structs.NetworkResource
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Items[0].Val); err != nil {
return err
}
if err := mapstructure.WeakDecode(m, &r); err != nil {
return err
}
var networkObj *ast.ObjectList
if ot, ok := o.Items[0].Val.(*ast.ObjectType); ok {
networkObj = ot.List
} else {
return fmt.Errorf("resource: should be an object")
}
if err := parsePorts(networkObj, &r); err != nil {
return err
}
result.Networks = []*structs.NetworkResource{&r}
}
return nil
}
示例3: parseResources
func parseResources(result *structs.Resources, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'resource' block allowed per task")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
delete(m, "network")
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
// Parse the network resources
if o := o.Get("network", false); o != nil {
if o.Len() > 1 {
return fmt.Errorf("only one 'network' resource allowed")
}
var r structs.NetworkResource
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
if err := mapstructure.WeakDecode(m, &r); err != nil {
return err
}
// Keep track of labels we've already seen so we can ensure there
// are no collisions when we turn them into environment variables.
// lowercase:NomalCase so we can get the first for the error message
seenLabel := map[string]string{}
for _, label := range r.DynamicPorts {
if !reDynamicPorts.MatchString(label) {
return errDynamicPorts
}
first, seen := seenLabel[strings.ToLower(label)]
if seen {
return fmt.Errorf("Found a port label collision: `%s` overlaps with previous `%s`", label, first)
} else {
seenLabel[strings.ToLower(label)] = label
}
}
result.Networks = []*structs.NetworkResource{&r}
}
}
return nil
}
示例4: validatePrimitive
func (m schemaMap) validatePrimitive(
k string,
raw interface{},
schema *Schema,
c *terraform.ResourceConfig) ([]string, []error) {
if c.IsComputed(k) {
// If the key is being computed, then it is not an error
return nil, nil
}
var decoded interface{}
switch schema.Type {
case TypeBool:
// Verify that we can parse this as the correct type
var n bool
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeInt:
// Verify that we can parse this as an int
var n int
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeFloat:
// Verify that we can parse this as an int
var n float64
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeString:
// Verify that we can parse this as a string
var n string
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
default:
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
}
if schema.ValidateFunc != nil {
return schema.ValidateFunc(decoded, k)
}
return nil, nil
}
示例5: Get
func (self *Zusaar) Get(baseurl, keyword, nickname string, places []string) ([]Event, error) {
var events []Event
for _, param := range []string{"keyword_or=" + url.QueryEscape(keyword), "owner_nickname=" + nickname, "nickname=" + nickname} {
for t := time.Now().Local(); t.Before(time.Now().Local().AddDate(0, 3, 0)); t = t.AddDate(0, 1, 0) {
query := param + "&ym=" + t.Format("200601")
result, err := GetJson(baseurl + query)
if err != nil {
return nil, err
}
if err = mapstructure.WeakDecode(result, &self.result); err != nil {
return nil, err
}
for _, e := range self.result.Event {
event := Event{
Id: e.Event_id,
Title: e.Title,
Started_at: format(e.Started_at),
Url: e.Event_url,
Summary: e.Catch,
Place: e.Address + "\n" + e.Place,
Description: parse(e.Description),
}
if event.Summary == "" {
event.Summary = trim(event.Description)
}
events = append(events, event)
}
time.Sleep(time.Duration(1 * time.Second))
}
}
return events, nil
}
示例6: parseProject
func parseProject(result *File, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'project' block allowed")
}
// Check for invalid keys
valid := []string{"name", "infrastructure"}
if err := checkHCLKeys(obj, valid); err != nil {
return multierror.Prefix(err, "project:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, obj); err != nil {
return err
}
// Parse the project
var proj Project
result.Project = &proj
if err := mapstructure.WeakDecode(m, &proj); err != nil {
return err
}
return nil
}
示例7: parseDetect
func parseDetect(result *Config, obj *hclobj.Object) error {
// 從map中獲取所有實際對象的key值
objects := make([]*hclobj.Object, 0, 2)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects = append(objects, o2)
}
}
if len(objects) == 0 {
return nil
}
// 檢查每個對象,返回實際結果
collection := make([]*Detector, 0, len(objects))
for _, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var d Detector
if err := mapstructure.WeakDecode(m, &d); err != nil {
return fmt.Errorf("解析detector錯誤 '%s' : %s", o.Key, err)
}
d.Type = o.Key
collection = append(collection, &d)
}
result.Detectors = collection
return nil
}
示例8: readPrimitive
func (r *DiffFieldReader) readPrimitive(
address []string, schema *Schema) (FieldReadResult, error) {
result, err := r.Source.ReadField(address)
if err != nil {
return FieldReadResult{}, err
}
attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
if !ok {
return result, nil
}
var resultVal string
if !attrD.NewComputed {
resultVal = attrD.New
if attrD.NewExtra != nil {
result.ValueProcessed = resultVal
if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
return FieldReadResult{}, err
}
}
}
result.Computed = attrD.NewComputed
result.Exists = true
result.Value, err = stringToPrimitive(resultVal, false, schema)
if err != nil {
return FieldReadResult{}, err
}
return result, nil
}
示例9: parseDetect
func parseDetect(result *Config, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make([]*hclobj.Object, 0, 2)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects = append(objects, o2)
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Detector, 0, len(objects))
for _, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var d Detector
if err := mapstructure.WeakDecode(m, &d); err != nil {
return fmt.Errorf(
"error parsing detector '%s': %s", o.Key, err)
}
d.Type = o.Key
collection = append(collection, &d)
}
result.Detectors = collection
return nil
}
示例10: parseAtlas
func parseAtlas(result **AtlasConfig, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'atlas' block allowed")
}
// Get our atlas object
listVal := list.Items[0].Val
// Check for invalid keys
valid := []string{
"infrastructure",
"token",
"join",
"endpoint",
}
if err := checkHCLKeys(listVal, valid); err != nil {
return err
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, listVal); err != nil {
return err
}
var atlas AtlasConfig
if err := mapstructure.WeakDecode(m, &atlas); err != nil {
return err
}
*result = &atlas
return nil
}
示例11: parseTLSConfig
func parseTLSConfig(result **config.TLSConfig, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'tls' block allowed")
}
// Get the TLS object
listVal := list.Items[0].Val
valid := []string{
"http",
"rpc",
"verify_server_hostname",
"ca_file",
"cert_file",
"key_file",
}
if err := checkHCLKeys(listVal, valid); err != nil {
return err
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, listVal); err != nil {
return err
}
var tlsConfig config.TLSConfig
if err := mapstructure.WeakDecode(m, &tlsConfig); err != nil {
return err
}
*result = &tlsConfig
return nil
}
示例12: parseLocalDisk
func parseLocalDisk(result **structs.LocalDisk, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'local_disk' block allowed")
}
// Get our local_disk object
obj := list.Items[0]
// Check for invalid keys
valid := []string{
"sticky",
"disk",
}
if err := checkHCLKeys(obj.Val, valid); err != nil {
return err
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, obj.Val); err != nil {
return err
}
var localDisk structs.LocalDisk
if err := mapstructure.WeakDecode(m, &localDisk); err != nil {
return err
}
*result = &localDisk
return nil
}
示例13: parseUpdate
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'update' block allowed per job")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
for _, key := range []string{"stagger", "Stagger"} {
if raw, ok := m[key]; ok {
switch v := raw.(type) {
case string:
dur, err := time.ParseDuration(v)
if err != nil {
return fmt.Errorf("invalid stagger time '%s'", raw)
}
m[key] = dur
case int:
m[key] = time.Duration(v) * time.Second
default:
return fmt.Errorf("invalid type for stagger time '%s'",
raw)
}
}
}
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
}
return nil
}
示例14: Run
func (c *AuditEnableCommand) Run(args []string) int {
var desc, id string
flags := c.Meta.FlagSet("audit-enable", FlagSetDefault)
flags.StringVar(&desc, "description", "", "")
flags.StringVar(&id, "id", "", "")
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) < 1 {
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\naudit-enable expects at least one argument: the type to enable"))
return 1
}
auditType := args[0]
if id == "" {
id = auditType
}
// Build the options
var stdin io.Reader = os.Stdin
if c.testStdin != nil {
stdin = c.testStdin
}
builder := &kvbuilder.Builder{Stdin: stdin}
if err := builder.Add(args[1:]...); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error parsing options: %s", err))
return 1
}
var opts map[string]string
if err := mapstructure.WeakDecode(builder.Map(), &opts); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error parsing options: %s", err))
return 1
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 1
}
err = client.Sys().EnableAudit(id, auditType, desc, opts)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error enabling audit backend: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf(
"Successfully enabled audit backend '%s'!", auditType))
return 0
}
示例15: parseUpdate
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'update' block allowed per job")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
for _, key := range []string{"stagger", "Stagger"} {
if raw, ok := m[key]; ok {
staggerTime, err := toDuration(raw)
if err != nil {
return fmt.Errorf("Invalid stagger time: %v", err)
}
m[key] = staggerTime
}
}
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
}
return nil
}