Skip to content

Commit

Permalink
Change google user interface mapping types
Browse files Browse the repository at this point in the history
The values returned by the Google Admin Directory API for the generic
lists does not have a specific type but are treated instead as basic
maps. As a consequence its fields are not capitalized.
  • Loading branch information
matteo-arella committed Feb 17, 2025
1 parent c1dfcc8 commit 6d28162
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 204 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ the engine that parses the template supports GoTemplate sintax with:

returns the first element of the list matching the given conditions:

For example `listFindFirst $list (dict "Primary" true)` returns the first element of the list whose `Primary` value is `true`.
For example `listFindFirst $list (dict "primary" true)` returns the first element of the list whose `primary` value is `true`.

## Local Usage

Expand Down
64 changes: 38 additions & 26 deletions internal/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,24 @@ func listFindFirstFun(list interface{}, search map[string]interface{}) interface
}

func interfaceFilterMatch(element interface{}, search map[string]interface{}) bool {
s := reflect.ValueOf(element)
for fieldName, searchValue := range search {
fieldValue := s.FieldByName(fieldName)
if !fieldValue.Equal(reflect.ValueOf(searchValue)) {
return false
elem := reflect.ValueOf(element)
switch elem.Kind() {
case reflect.Map:
elemMap := elem.Interface().(map[string]interface{})
for fieldName, searchValue := range search {
if fieldValue, ok := elemMap[fieldName]; !ok || fieldValue != searchValue {
return false
}
}
case reflect.Struct:
for fieldName, searchValue := range search {
fieldValue := elem.FieldByName(fieldName)
if !fieldValue.IsValid() || fieldValue.Interface() != searchValue {
return false
}
}
default:
return false
}
return true
}
Expand Down Expand Up @@ -142,48 +154,48 @@ func NewMapper(userTemplate string) (UserMapper, error) {
"displayName": {{ list .GivenName .FamilyName | join " " | trim | quote }},
{{- end -}}
{{- with .Websites -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with .Value -}}"ProfileUrl": {{ . | quote }},{{- end -}}
{{- with default (first .) (listFindFirst . (dict "primary" true)) -}}
{{- with .value -}}"profileUrl": {{ . | quote }},{{- end -}}
{{- end -}}
{{- end -}}
{{- with .Emails -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with default (first .) (listFindFirst . (dict "primary" true)) -}}
"emails": [{
{{- with .Address -}}"value": {{ . | quote }},{{- end -}}
"type": {{ default "work" (.Type | quote) }},
{{- with .address -}}"value": {{ . | quote }},{{- end -}}
"type": {{ default "work" .type | quote }},
"primary": true
}],
{{- end -}}
{{- end -}}
{{- with .Addresses -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with default (first .) (listFindFirst . (dict "primary" true)) -}}
"addresses": [{
{{- with .StreetAddress -}}"streetAddress": {{ . | quote }},{{- end -}}
{{- with .Locality -}}"locality": {{ . | quote }},{{- end -}}
{{- with .Region -}}"region": {{ . | quote }},{{- end -}}
{{- with .PostalCode -}}"postalCode": {{ . | quote }},{{- end -}}
{{- with .Country -}}"country": {{ . | quote }},{{- end -}}
{{- with .Formatted -}}"formatted": {{ . | quote }},{{- end -}}
"type": {{ default "work" (.Type | quote) }},
{{- with .streetAddress -}}"streetAddress": {{ . | quote }},{{- end -}}
{{- with .locality -}}"locality": {{ . | quote }},{{- end -}}
{{- with .region -}}"region": {{ . | quote }},{{- end -}}
{{- with .postalCode -}}"postalCode": {{ . | quote }},{{- end -}}
{{- with .country -}}"country": {{ . | quote }},{{- end -}}
{{- with .formatted -}}"formatted": {{ . | quote }},{{- end -}}
"type": {{ default "work" .type | quote }},
"primary": true
}],
{{- end -}}
{{- end -}}
{{- with .Phones -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with default (first .) (listFindFirst . (dict "primary" true)) -}}
"phoneNumbers": [{
{{- with .Value -}}"value": {{ . | quote }},{{- end -}}
"type": {{ default "work" (.Type | quote) }}
{{- with .value -}}"value": {{ . | quote }},{{- end -}}
"type": {{ default "work" .type | quote }}
}],
{{- end -}}
{{- end -}}
{{- with .Organizations -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with default (first .) (listFindFirst . (dict "primary" true)) -}}
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": {
{{- with .Name -}}"organization": {{ . | quote }},{{- end -}}
{{- with .CostCenter -}}"costCenter": {{ . | quote }},{{- end -}}
{{- with .Department -}}"department": {{ . | quote }},{{- end -}}
{{- with .Domain -}}"division": {{ . | quote }},{{- end -}}
{{- with .name -}}"organization": {{ . | quote }},{{- end -}}
{{- with .costCenter -}}"costCenter": {{ . | quote }},{{- end -}}
{{- with .department -}}"department": {{ . | quote }},{{- end -}}
{{- with .domain -}}"division": {{ . | quote }},{{- end -}}
"employeeNumber": {{ $.Id | quote }}
},
{{- $userSchemas = append $userSchemas "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" -}}
Expand Down
Loading

0 comments on commit 6d28162

Please sign in to comment.