Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

jsonParameter parser works counterintuitive #1

Open
1 task done
souljorje opened this issue Feb 23, 2022 · 10 comments
Open
1 task done

jsonParameter parser works counterintuitive #1

souljorje opened this issue Feb 23, 2022 · 10 comments
Assignees

Comments

@souljorje
Copy link
Contributor

souljorje commented Feb 23, 2022

  • I'm submitting a ...

  • feature request

  • Summary

I wish it works like this:

contractsGetBigMapByNameKeys(
    address,
    'name',
    {
      value: {
        token_id: {
           in: ['0', '1'],
        },
       /*  instead of
       in: {
          jsonPath: 'token_id',
          jsonValue: ['0', '1'],
        },
       */
      },
    },
  )
  • Other information

I suggest to recursively create that.kind.of.path from object with last nested property as value. And also I suggest to use reduce for declarativeness.

@souljorje souljorje changed the title jsonParameter parser doesn't work jsonParameter parser works counterintuitive Feb 23, 2022
@souljorje
Copy link
Contributor Author

@Groxan @m-kus what do u think dudes

@Groxan
Copy link
Collaborator

Groxan commented Feb 23, 2022

@souljorje how would you specify this path: ?value.field.array.[*].field2=...?

@souljorje
Copy link
Contributor Author

souljorje commented Feb 23, 2022

@Groxan

{
  value: {
  	field: {
      array: {
        '[*]': {
        	field2: {
        		eq: 'foo',
				// also I could specify at one place i.e.
				ne: 'bar',
				// etc
        	}
        }
      }
    }
  }
}

@Groxan
Copy link
Collaborator

Groxan commented Feb 23, 2022

Actually, would be cool to use proxy instead, and just write it like value.toke_id.in = [0, 1] and then build path in proxy handler, but it's still not clear what to do with [*] (value.array['*'].field=123 is not really elegant).

@souljorje
Copy link
Contributor Author

souljorje commented Feb 23, 2022

@Groxan my suggestion is kinda mongodb query syntax: nested & intuitive imho 🤷‍♂️

proxy for what?

@Groxan
Copy link
Collaborator

Groxan commented Feb 23, 2022

Proxy is used for creating dynamic object, so that you can "access" fields that are not defined. Nesting is good because you can merge multiple parameters (paths) into a single object, but it's less handy to write, especially in case of long and complex paths.

Anyway, I don't object. Just my thoughts.

@mv-go
Copy link
Contributor

mv-go commented Feb 23, 2022

Another issue that I see is constructing a query for a response object, that has in | ne | etc. in its fields. Such objects might not YET exist, but they might be there in the future. For the current proposal, the parameter would look something like this

value: {
  someField: {
    in: {
      in: string[]
    }
  }
}

No sure if it improves readability, not mentioning the complexity of creating a QS parser for such parameter.

@mv-go
Copy link
Contributor

mv-go commented Feb 23, 2022

Another issue is creating a type for this parameter. The difficulty would be describing a Record of Records of unknown depth with the deepest level always being strongly typed to available eq, ne, in, etc. types.

@m-kus
Copy link
Contributor

m-kus commented Feb 23, 2022

@souljorje this can be done only knowing the particular contract storage type. We will definitely implement that in TzKT wrappers

@souljorje
Copy link
Contributor Author

souljorje commented Feb 24, 2022

Here's a parser for suggested syntax.

const isPlainObject = (v) => {
  if (Object.prototype.toString.call(v) !== '[object Object]') return false;
  const prototype = Object.getPrototypeOf(v);
  return prototype === null || prototype === Object.prototype;
};

const objectToQueryObject = (obj, path) => {
  return Object.entries(obj).reduce((acc, [key, value]) => {
    const newPath = path ? `${path}.${key}` : key;
    if (isPlainObject(value)) {
      return {
        ...acc,
        ...objectToQueryObject(value, newPath)
      };
    }
    acc[newPath] = value;
    return acc;
  }, {})
};

const queryObjectRaw = {
  value: {
    field: {
      array: {
        '[*]': {
          field2: {
            eq: 'foo',
            ne: 'bar',
          }
        }
      },
      someOtherProp: { 
		in: [1, 2, 3],
        ne: 'baz',
      }
    },
    someOtherField: {
      gt: 123
    },
  },
  otherValue: {
    eq: 2
  },
};

const result = objectToQueryObject(queryObjectRaw);

console.log('result', result);

const queryString = new URLSearchParams(result).toString();

console.log('queryString', queryString);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants