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

random, encode, decode incoherence #901

Closed
yovanoc opened this issue Dec 9, 2023 · 2 comments
Closed

random, encode, decode incoherence #901

yovanoc opened this issue Dec 9, 2023 · 2 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@yovanoc
Copy link

yovanoc commented Dec 9, 2023

From just the main example on the docs, and added random and protobuf I observed strange behaviors with each launch.

code:

import typia, { tags } from "typia";

export const encode = typia.protobuf.createEncode<Customer>();
export const decode = typia.protobuf.createDecode<Customer>();

type Customer = {
    id: number & tags.Type<"int32">;
    email: string & tags.Format<"email">;
    name: string;
    pet: null | Cat | Dog;
    memo: null | Map<string, string>;
    logins: Array<CustomerLogin>;
}

type Cat = {
    type: "cat";
    name: string;
    ribbon: boolean;
}
type Dog = {
    type: "dog";
    name: string;
    hunt: boolean;
}
type CustomerLogin = {
    success: boolean;
    href: string & tags.Format<"url">;
    referrer: string & tags.Format<"url">;
    ip: string & (tags.Format<"ipv4"> | tags.Format<"ipv6">);
    time: string & tags.Format<"date-time">;
}

const customer = typia.random<Customer>();
console.dir(customer, { depth: 10 });
const encoded = encode(customer);
console.log(encoded);
const decoded = decode(encoded);
console.dir(decoded, { depth: 10 });

console.log(typia.is<Customer>(decoded));
console.log(typia.is<Customer>({}));

logs:
1.

{
  id: 73,
  email: 'fpjnengcpe@iwxjueqzvk.eam',
  name: 'oedanvhbpu',
  pet: null,
  memo: Map(0) {},
  logins: [
    {
      success: true,
      href: 'https://eknfprhszr.oqn',
      referrer: 'https://dnzktcyryi.yws',
      ip: '85.88.164.201',
      time: '2023-11-29T14:23:26.578Z'
    },
    {
      success: true,
      href: 'https://pmyzpjycky.plc',
      referrer: 'https://jeogfqxsid.uih',
      ip: '104.222.199.180',
      time: '2023-11-16T18:56:46.797Z'
    },
    {
      success: false,
      href: 'https://mkjkuwnrmn.doa',
      referrer: 'https://mjmopnpjlc.bzx',
      ip: '207.48.164.65',
      time: '2023-11-25T06:21:38.065Z'
    }
  ]
}
Uint8Array(322) [
    8,  73,  18,  25, 102, 112, 106, 110, 101, 110, 103,  99,
  112, 101,  64, 105, 119, 120, 106, 117, 101, 113, 122, 118,
  107,  46, 101,  97, 109,  26,  10, 111, 101, 100,  97, 110,
  118, 104,  98, 112, 117,  58,  91,   8,   1,  18,  22, 104,
  116, 116, 112, 115,  58,  47,  47, 101, 107, 110, 102, 112,
  114, 104, 115, 122, 114,  46, 111, 113, 110,  26,  22, 104,
  116, 116, 112, 115,  58,  47,  47, 100, 110, 122, 107, 116,
   99, 121, 114, 121, 105,  46, 121, 119, 115,  34,  13,  56,
   53,  46,  56,  56,
  ... 222 more items
]
{
  id: 73,
  email: 'fpjnengcpe@iwxjueqzvk.eam',
  name: 'oedanvhbpu',
  pet: null,
  memo: null,
  logins: [
    {
      success: true,
      href: 'https://eknfprhszr.oqn',
      referrer: 'https://dnzktcyryi.yws',
      ip: '85.88.164.201',
      time: '2023-11-29T14:23:26.578Z'
    },
    {
      success: true,
      href: 'https://pmyzpjycky.plc',
      referrer: 'https://jeogfqxsid.uih',
      ip: '104.222.199.180',
      time: '2023-11-16T18:56:46.797Z'
    },
    {
      success: false,
      href: 'https://mkjkuwnrmn.doa',
      referrer: 'https://mjmopnpjlc.bzx',
      ip: '207.48.164.65',
      time: '2023-11-25T06:21:38.065Z'
    }
  ]
}
true
false

here the logins are correct but memo is null after decode but was created empty with the random

{
  id: 80,
  email: 'exkoehluri@huvydybhxg.kyq',
  name: 'rxluh',
  pet: { type: 'cat', name: 'grsmtd', ribbon: true },
  memo: null,
  logins: [
    {
      success: true,
      href: 'https://mzyxgivgek.aor',
      referrer: 'https://popdrjqlfu.djl',
      ip: '180.177.235.68',
      time: '2023-11-20T21:54:49.531Z'
    },
    {
      success: false,
      href: 'https://murphytwex.due',
      referrer: 'https://zmyucnipfc.gnd',
      ip: 'd9f4:98bb:727e:d50:8182:bef2:c218:59ee',
      time: '2023-11-23T06:39:45.568Z'
    }
  ]
}
Uint8Array(53) [
    8,  80,  18,  25, 101, 120, 107, 111, 101, 104,
  108, 117, 114, 105,  64, 104, 117, 118, 121, 100,
  121,  98, 104, 120, 103,  46, 107, 121, 113,  26,
    5, 114, 120, 108, 117, 104,  34,  15,  10,   3,
   99,  97, 116,  18,   6, 103, 114, 115, 109, 116,
  100,  24,   1
]
{
  id: 80,
  email: 'exkoehluri@huvydybhxg.kyq',
  name: 'rxluh',
  pet: { type: 'cat', name: 'grsmtd', ribbon: true },
  memo: null,
  logins: []
}
true
false

here it completely ignore logins..

I don't know what is going on, but maybe someone can explain this to me.

thanks

@samchon samchon self-assigned this Dec 11, 2023
@samchon samchon added question Further information is requested bug Something isn't working labels Dec 11, 2023
@samchon
Copy link
Owner

samchon commented Dec 11, 2023

Good Point.

Fixed the bug that stopping the encoding when union type comes. Upgrade to the v5.3.5 version please.

By the way, Protocol Buffer does not write any data when undefined, null and Array.length := 0 value comes. Therefore, no way to distinguish which empty value being used when Array | undefined | null type comes. It is not a bug, but spec of typia library considering such characteristics of Protocol Buffer spec.

@yovanoc
Copy link
Author

yovanoc commented Dec 11, 2023

Yes, you are right about protobuf. v5.3.5 works great!

samchon added a commit that referenced this issue Dec 11, 2023
Fix #901 - `protobuf.encode<T>()` be returned when union type comes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants