MCPcopy
hub / github.com/apache/casbin / GetFieldIndex

Method GetFieldIndex

model/model.go:409–436  ·  view source on GitHub ↗
(ptype string, field string)

Source from the content-addressed store, hash-verified

407}
408
409func (model Model) GetFieldIndex(ptype string, field string) (int, error) {
410 assertion := model["p"][ptype]
411
412 assertion.FieldIndexMutex.RLock()
413 if index, ok := assertion.FieldIndexMap[field]; ok {
414 assertion.FieldIndexMutex.RUnlock()
415 return index, nil
416 }
417 assertion.FieldIndexMutex.RUnlock()
418
419 pattern := fmt.Sprintf("%s_"+field, ptype)
420 index := -1
421 for i, token := range assertion.Tokens {
422 if token == pattern {
423 index = i
424 break
425 }
426 }
427 if index == -1 {
428 return index, fmt.Errorf(field + " index is not set, please use enforcer.SetFieldIndex() to set index")
429 }
430
431 assertion.FieldIndexMutex.Lock()
432 assertion.FieldIndexMap[field] = index
433 assertion.FieldIndexMutex.Unlock()
434
435 return index, nil
436}

Callers 10

GetAllNamedSubjectsMethod · 0.45
GetAllNamedObjectsMethod · 0.45
GetAllNamedActionsMethod · 0.45
DeleteUserCtxMethod · 0.45
DeleteRoleCtxMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected