github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/helper/schema/field_schema.go (about)

     1  package schema
     2  
     3  // FieldSchema is a basic schema to describe the format of a path field.
     4  type FieldSchema struct {
     5  	Type        FieldType
     6  	Default     interface{}
     7  	Description string
     8  }
     9  
    10  // DefaultOrZero returns the default value if it is set, or otherwise
    11  // the zero value of the type.
    12  func (s *FieldSchema) DefaultOrZero() interface{} {
    13  	if s.Default != nil {
    14  		return s.Default
    15  	}
    16  
    17  	return s.Type.Zero()
    18  }
    19  
    20  func (t FieldType) Zero() interface{} {
    21  	switch t {
    22  	case TypeString:
    23  		return ""
    24  	case TypeInt:
    25  		return 0
    26  	case TypeBool:
    27  		return false
    28  	case TypeMap:
    29  		return map[string]interface{}{}
    30  	default:
    31  		panic("unknown type: " + t.String())
    32  	}
    33  }