Skip to content

SDKS-5410. Create attributes cache in memory and add it to browser cl… - #66

Merged
emmaz90 merged 7 commits into
attributes_bindingfrom
attributes_binding_storage
Jan 14, 2022
Merged

SDKS-5410. Create attributes cache in memory and add it to browser cl…#66
emmaz90 merged 7 commits into
attributes_bindingfrom
attributes_binding_storage

Conversation

@emmaz90

@emmaz90 emmaz90 commented Jan 7, 2022

Copy link
Copy Markdown
Contributor

Javascript commons library

What did you accomplish?

created attributes in memory cache and attributes decorator for browser client

How do we test the changes introduced in this PR?

Added unit tests

Extra Notes

Comment thread src/types.ts Outdated
/**
* Remove all the stored attributes in the client's in memory attribute storage
*/
clearAttributes(): any

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearAttributes(): any --> clearAttributes(): boolean

expect(cache.getAttribute('attributeName3')).toEqual('attributeValue3');
expect(cache.getAttribute('attributeName4')).toEqual('attributeValue4');

cache.clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(cache.clear()).toEqual(true);

import { AttributesCacheInMemory } from '../AttributesCacheInMemory';


describe('ATTRIBUTES CACHE', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the describe call if there is only one test inside.



test('INPUT VALIDATION for Attribute', () => {
expect(validateAttribute(loggerMock, '', 'empty', 'some_method_attrs')).toEqual(false); // It should be invalid if the attribute key is not a string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emma, it seems that there are more asserts here that we can also migrate.

test('Should return false and log error if attributes map is invalid', () => {

expect(validateAttributesDeep(loggerMock, { '': 'empty' }, 'some_method_attrs')).toEqual(false); // It should be invalid if the attribute key is not a string
expect(validateAttributesDeep(loggerMock, { 'attributeKey': new Date() }, 'some_method_attrs')).toEqual(false); // It should be invalid if the attribute value is not a String, Number, Boolean or Lists.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migrate the tape assert assert.equals(validateAttributesDeep({'attrKey': null}, 'some_method_attrs'), false, 'should be invalid if the attribute value is not a String, Number, Boolean or Lists.');

/**
* Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call
*/
export function ClientAttributesDecoration<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(log: ILogger, client: TClient): any {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to lower case: clientAttributesDecoration. For convention, we use lowercase for factory functions (E.g.: = myFactory()) and uppercase for classes -a.k.a constructor functions- (E.g., = new MyClass())

* @param {string, number, boolean, list} attributeValue Attribute value
* @returns {boolean} true if the attribute was stored and false otherways
*/
function setAttribute(attributeName: string, attributeValue: Object): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to use the Typescript automatic type inference as much as possible. For example:

  • Do let x = 10, instead of let x: number = 10
  • In this case, remove the return type of the method, since if should be inferred automatically: function setAttribute(attributeName: string, attributeValue: Object) {

Comment on lines +111 to +116
setAttribute: setAttribute,
getAttribute: getAttribute,
setAttributes: setAttributes,
getAttributes: getAttributes,
removeAttribute: removeAttribute,
clearAttributes: clearAttributes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these methods can be defined here directly, to reduce some size. For example, replace

setAttributes: setAttributes,

with

    /**
     * Add an attribute to client's in memory attributes storage
     * 
     * @param {string} attributeName Attrinute name
     * @param {string, number, boolean, list} attributeValue Attribute value
     * @returns {boolean} true if the attribute was stored and false otherways
     */
    setAttribute(attributeName: string, attributeValue: Object) {
      const attribute: Record<string, Object> = {};
      attribute[attributeName] = attributeValue;
      if (!validateAttributesDeep(log, attribute, 'setAttribute')) return false;
      log.debug(`stored ${attributeValue} for attribute ${attributeName}`);
      return attributeStorage.setAttribute(attributeName, attributeValue);
    },

expect(client.getAttribute('attributeName3')).toEqual('attributeValue3');
expect(client.getAttribute('attributeName4')).toEqual('attributeValue4');

client.clearAttributes();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(client.clearAttributes()).toBe(true);

expect(emmanuelClient.getAttributes()).toEqual({ name: 'Emmanuel', email: 'emmanuel@split.io' });
expect(emilianoClient.getAttributes()).toEqual({ name: 'Emiliano', email: 'emiliano@split.io' });

emmanuelClient.clearAttributes();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(emmanuelClient.clearAttributes()).toBe(true);

@EmilianoSanchez EmilianoSanchez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@emmaz90
emmaz90 merged commit d2a5e26 into attributes_binding Jan 14, 2022
@emmaz90
emmaz90 deleted the attributes_binding_storage branch January 14, 2022 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants