Skip to content

Commit

Permalink
Merge pull request #8 from ivixvi/refactor/tests-for-scope
Browse files Browse the repository at this point in the history
refactor: scope
  • Loading branch information
ivixvi authored Jul 11, 2024
2 parents 740244c + b0803d8 commit 61129de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
48 changes: 32 additions & 16 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (n *ScopeNavigator) ApplyScopedMap(scopedMap scim.ResourceAttributes) {
}

data := n.data
uriPrefix, containsURI := containsURIPrefix(n.op.Path)
uriPrefix, containsURI := n.containsURIPrefix()
if containsURI {
if len(uriScoped) == 0 {
delete(data, uriPrefix)
Expand All @@ -58,33 +58,49 @@ func (n *ScopeNavigator) ApplyScopedMap(scopedMap scim.ResourceAttributes) {
// getURIScopedMap は URIに応じて、処理対象のMapを返却します
func (n *ScopeNavigator) getURIScopedMap() scim.ResourceAttributes {
uriScoped := n.data
uriPrefix, containsURI := containsURIPrefix(n.op.Path)
if containsURI {
data_, ok := n.data[uriPrefix].(map[string]interface{})
switch ok {
case true:
uriScoped = data_
case false:
uriScoped = scim.ResourceAttributes{}
}
}
uriPrefix, ok := n.containsURIPrefix()
uriScoped = n.navigateToMap(uriScoped, uriPrefix, ok)
return uriScoped
}

// getAttributeScopedMap は 属性に応じて、処理対象のMapを返却します
func (n *ScopeNavigator) getAttributeScopedMap() (scim.ResourceAttributes, string) {
// initialize returns
attrName := n.attr.Name()
data := n.getURIScopedMap()
if n.attr.HasSubAttributes() && n.op.Path != nil && n.op.Path.AttributePath.SubAttribute != nil {
data_, ok := data[n.op.Path.AttributePath.AttributeName].(map[string]interface{})
subAttrName, ok := n.requiredSubAttributes()
data = n.navigateToMap(data, n.attr.Name(), ok)
return data, subAttrName
}

func (n *ScopeNavigator) navigateToMap(data map[string]interface{}, attr string, ok bool) scim.ResourceAttributes {
if ok {
data_, ok := data[attr].(map[string]interface{})
switch ok {
case true:
data = data_
attrName = *n.op.Path.AttributePath.SubAttribute
case false:
data = scim.ResourceAttributes{}
}
}
return data, attrName
return data
}

func (n *ScopeNavigator) containsURIPrefix() (string, bool) {
ok := false
uriPrefix := ""
if n.op.Path != nil && n.op.Path.AttributePath.URIPrefix != nil {
ok = true
uriPrefix = *n.op.Path.AttributePath.URIPrefix
}
return uriPrefix, ok
}

func (n *ScopeNavigator) requiredSubAttributes() (string, bool) {
ok := false
subAttr := n.attr.Name()
if n.attr.HasSubAttributes() && n.op.Path != nil && n.op.Path.AttributePath.SubAttribute != nil {
ok = true
subAttr = *n.op.Path.AttributePath.SubAttribute
}
return subAttr, ok
}
11 changes: 0 additions & 11 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scimpatch
import (
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/schema"
filter "github.com/scim2/filter-parser/v2"
)

var (
Expand All @@ -22,13 +21,3 @@ func isImmutable(op string, attr schema.CoreAttribute) bool {
func isReadOnly(attr schema.CoreAttribute) bool {
return attr.Mutability() == attributeMutabilityReadOnly
}

func containsURIPrefix(path *filter.Path) (string, bool) {
ok := false
uriPrefix := ""
if path != nil && path.AttributePath.URIPrefix != nil {
ok = true
uriPrefix = *path.AttributePath.URIPrefix
}
return uriPrefix, ok
}

0 comments on commit 61129de

Please sign in to comment.