Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GlobalDecoderRegistry, better handling with fields with 'accepted_interface' option #526

Merged
merged 33 commits into from
Nov 19, 2023

Conversation

Zetazzz
Copy link
Collaborator

@Zetazzz Zetazzz commented Nov 19, 2023

Added interfaces.useGlobalDecoderRegistry
enables GlobalDecoderRegistry and related functions. Highly recommended to enable when dealing with fields with 'accepted_interface' option.

Changes included:

Build without error.

is method, to test if an object or json implements the interface:

export const SendAuthorization = {
  typeUrl: "/cosmos.bank.v1beta1.SendAuthorization",
  is(o: any): o is SendAuthorization {
    return o && (o.$typeUrl === SendAuthorization.typeUrl || Array.isArray(o.spendLimit) && (!o.spendLimit.length || Coin.is(o.spendLimit[0])));
  },
  isSDK(o: any): o is SendAuthorizationSDKType {
    return o && (o.$typeUrl === SendAuthorization.typeUrl || Array.isArray(o.spend_limit) && (!o.spend_limit.length || Coin.isSDK(o.spend_limit[0])));
  },
...
}

GlobalDecoderRegistry, all codecs are registered here:

import { GlobalDecoderRegistry } from "./registry";

// get a codec by a typeUrl as a key
GlobalDecoderRegistry.getDecoder(key)
// or get a codec by an instance, it'll get by using $typeUrl field or the pattern of fields.
GlobalDecoderRegistry.getDecoderByInstance(obj)

Better handling polymorphic fields, samples:

decode(…): Grant {
        …
        case 1:
          // will also decode accepted types(e.g. GenericAuthorization | DepositDeploymentAuthorization | SendAuthorization) from Any
          message.authorization = GlobalDecoderRegistry.unwrapAny(reader);
}
  fromJSON(object: any): Grant {
    const obj = createBaseGrant();
    // auto detect right codec for object.authorization, and then get object from json.
    if (isSet(object.authorization)) obj.authorization = GlobalDecoderRegistry.fromJSON(object.authorization);
    return obj;
  },

Please see 'packages/telescope/tests/impl-interfaces.test.ts' for more detailed usage.

@Zetazzz Zetazzz merged commit 7b91b83 into main Nov 19, 2023
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.

1 participant