TokenizeIdentityField tokenizes a specific field in an identity. Parameters: - identityID string: The ID of the identity. - fieldName string: The name of the field to tokenize. Returns: - error: An error if the field could not be tokenized.
(identityID, fieldName string)
| 149 | // Returns: |
| 150 | // - error: An error if the field could not be tokenized. |
| 151 | func (l *LedgerForge) TokenizeIdentityField(identityID, fieldName string) error { |
| 152 | // Convert field name to struct field format for reflection |
| 153 | structFieldName := convertToStructFieldName(fieldName) |
| 154 | |
| 155 | // Check if field is tokenizable |
| 156 | validField := false |
| 157 | for _, field := range tokenization.TokenizableFields { |
| 158 | if field == structFieldName { |
| 159 | validField = true |
| 160 | break |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if !validField { |
| 165 | return fmt.Errorf("field %s is not tokenizable", fieldName) |
| 166 | } |
| 167 | |
| 168 | // Get the identity |
| 169 | identity, err := l.GetIdentity(identityID) |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | |
| 174 | // Check if field is already tokenized using the original field name |
| 175 | // as IsFieldTokenized will handle the conversion internally |
| 176 | if identity.IsFieldTokenized(fieldName) { |
| 177 | return fmt.Errorf("field %s is already tokenized", fieldName) |
| 178 | } |
| 179 | |
| 180 | // Get the field value using reflection with struct field name |
| 181 | val := reflect.ValueOf(identity).Elem() |
| 182 | fieldVal := val.FieldByName(structFieldName) |
| 183 | |
| 184 | if !fieldVal.IsValid() || !fieldVal.CanSet() { |
| 185 | return fmt.Errorf("field %s not found or cannot be set", fieldName) |
| 186 | } |
| 187 | |
| 188 | // Get the string value |
| 189 | strVal := fieldVal.String() |
| 190 | |
| 191 | // Tokenize the value |
| 192 | token, err := l.tokenizer.TokenizeWithMode(strVal, tokenization.FormatPreservingMode) |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | |
| 197 | // Set the tokenized value |
| 198 | fieldVal.SetString(token) |
| 199 | |
| 200 | // Mark the field as tokenized using the original field name |
| 201 | // as MarkFieldAsTokenized will handle the conversion internally |
| 202 | identity.MarkFieldAsTokenized(fieldName) |
| 203 | |
| 204 | // Update the identity |
| 205 | return l.UpdateIdentity(identity) |
| 206 | } |
| 207 | |
| 208 | // DetokenizeIdentityField detokenizes a specific field in an identity. |