MCPcopy Create free account
hub / github.com/cloudwan/gohan / Init

Method Init

schema/schema.go:230–279  ·  view source on GitHub ↗

Init initializes schema

()

Source from the content-addressed store, hash-verified

228
229// Init initializes schema
230func (schema *Schema) Init() error {
231 if schema.IsAbstract() {
232 return nil
233 }
234 jsonSchema := schema.JSONSchema
235 parent := schema.Parent
236
237 required := util.MaybeStringList(jsonSchema["required"])
238 properties := util.MaybeMap(jsonSchema["properties"])
239 indexes := util.MaybeMap(jsonSchema["indexes"])
240 propertiesOrder := util.MaybeStringList(jsonSchema["propertiesOrder"])
241 if parent != "" && properties[FormatParentID(parent)] == nil {
242 properties[FormatParentID(parent)] = getParentPropertyObj(parent, parent)
243 propertiesOrder = append(propertiesOrder, FormatParentID(parent))
244 required = append(required, FormatParentID(parent))
245 }
246
247 jsonSchema["required"] = required
248
249 schema.JSONSchemaOnCreate = filterSchemaByPermission(jsonSchema, "create")
250 schema.JSONSchemaOnUpdate = filterSchemaByPermission(jsonSchema, "update")
251
252 schema.Properties = []Property{}
253 for id, property := range properties {
254 propertyRequired := util.ContainsString(required, id)
255 propertyObj := NewPropertyFromObj(id, property, propertyRequired)
256 schema.Properties = append(schema.Properties, *propertyObj)
257 }
258
259 schema.Indexes = []Index{}
260 for name, property := range indexes {
261 indexObj, err := NewIndexFromObj(name, property)
262 if err != nil {
263 return fmt.Errorf("Invalid schema: err: %v", err)
264 }
265 schema.Indexes = append(schema.Indexes, *indexObj)
266 }
267
268 order := PropertyOrder{propertiesOrder: propertiesOrder, properties: schema.Properties}
269 sort.Sort(order)
270
271 for _, property := range schema.Properties {
272 if !util.ContainsString(propertiesOrder, property.ID) {
273 propertiesOrder = append(propertiesOrder, property.ID)
274 }
275 }
276 jsonSchema["propertiesOrder"] = propertiesOrder
277
278 return nil
279}
280
281// IsAbstract checks if this schema is abstract or not
282func (schema *Schema) IsAbstract() bool {

Callers 3

newSchemaFromObjFunction · 0.95
ExtendMethod · 0.95
connectDBMethod · 0.80

Calls 8

IsAbstractMethod · 0.95
FormatParentIDFunction · 0.85
getParentPropertyObjFunction · 0.85
filterSchemaByPermissionFunction · 0.85
NewPropertyFromObjFunction · 0.85
NewIndexFromObjFunction · 0.85
MaybeMapMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected