github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/schema/property.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package schema
    17  
    18  //Property is a definition of each Property
    19  type Property struct {
    20  	ID, Title, Description string
    21  	Type, Format           string
    22  	Properties             map[string]interface{}
    23  	Relation               string
    24  	RelationProperty       string
    25  	Unique                 bool
    26  	Nullable               bool
    27  	SQLType                string
    28  	Default                interface{}
    29  }
    30  
    31  //PropertyMap is a map of Property
    32  type PropertyMap map[string]Property
    33  
    34  //NewProperty is a constructor for Property type
    35  func NewProperty(id, title, description, typeID, format, relation, relationProperty, sqlType string, unique, nullable bool, properties map[string]interface{}, defaultValue interface{}) Property {
    36  	Property := Property{
    37  		ID:               id,
    38  		Title:            title,
    39  		Format:           format,
    40  		Description:      description,
    41  		Type:             typeID,
    42  		Relation:         relation,
    43  		RelationProperty: relationProperty,
    44  		Unique:           unique,
    45  		Nullable:         nullable,
    46  		Default:          defaultValue,
    47  		Properties:       properties,
    48  		SQLType:          sqlType,
    49  	}
    50  	return Property
    51  }
    52  
    53  //NewPropertyFromObj make Property  from obj
    54  func NewPropertyFromObj(id string, rawTypeData interface{}, required bool) (*Property, error) {
    55  	typeData := rawTypeData.(map[string]interface{})
    56  	title, _ := typeData["title"].(string)
    57  	description, _ := typeData["description"].(string)
    58  	var typeID string
    59  	nullable := false
    60  	switch typeData["type"].(type) {
    61  	case string:
    62  		typeID = typeData["type"].(string)
    63  	case []interface{}:
    64  		for _, typeInt := range typeData["type"].([]interface{}) {
    65  			// type can be either string or list of string. we allow for any type and optional null
    66  			// in order to retrieve right type, we need to skip null
    67  			if typeInt.(string) != "null" {
    68  				typeID = typeInt.(string)
    69  			} else {
    70  				nullable = true
    71  			}
    72  		}
    73  	}
    74  	format, _ := typeData["format"].(string)
    75  	relation, _ := typeData["relation"].(string)
    76  	relationProperty, _ := typeData["relation_property"].(string)
    77  	unique, _ := typeData["unique"].(bool)
    78  	properties, _ := typeData["properties"].(map[string]interface{})
    79  	defaultValue, _ := typeData["default"]
    80  	if !required && defaultValue == nil {
    81  		nullable = true
    82  	}
    83  	sqlType, _ := typeData["sql"].(string)
    84  	Property := NewProperty(id, title, description, typeID, format, relation, relationProperty, sqlType, unique, nullable, properties, defaultValue)
    85  	return &Property, nil
    86  }