MLopezJ / LWM2M-JSONSchema

LWM2M protocol From XML to JSON Schema
0 stars 0 forks source link

Location object is not unique #2

Open coderbyheart opened 1 year ago

coderbyheart commented 1 year ago

Using the Name of the object as the shadow key is not conflict free, right now two objects are defined in the registry with that name:

  1. https://github.com/OpenMobileAlliance/lwm2m-registry/blob/0b8eb0e7c1e5db0492aa3d9015476b63cd34f1cf/6.xml#L63
  2. https://github.com/OpenMobileAlliance/lwm2m-registry/blob/0b8eb0e7c1e5db0492aa3d9015476b63cd34f1cf/3336.xml#L36
coderbyheart commented 1 year ago
import { readdir, readFile } from "node:fs/promises";
import { XMLParser } from "fast-xml-parser";
import path from "node:path";

// git clone https://github.com/OpenMobileAlliance/lwm2m-registry --depth 1 registry
const files = (await readdir(path.join(process.cwd(), "registry"))).filter(
  (f) => f.endsWith(".xml")
);

const parser = new XMLParser();

const registry = {};

for (const file of files) {
  const { Name, ObjectURN } =
    parser.parse(
      await readFile(path.join(process.cwd(), "registry", file), "utf-8")
    )?.LWM2M?.Object ?? {};
  if (Name === undefined) continue;
  if (registry[Name] !== undefined) {
    console.error(`Conflict: ${Name}!`, ObjectURN, registry[Name]);
  }
  registry[Name] = ObjectURN;
}
MLopezJ commented 1 year ago

This is correct.

In order to prevent conflicts, changes were implemented and now the description of the data is expecting the key of the registry as the key of the object.

Registry:

<Object ObjectType="MODefinition">
      <Name>LwM2M Server</Name>
      <Description1>...></Description1>
      <ObjectID>1</ObjectID>
      <ObjectURN>urn:oma:lwm2m:oma:1:1.2</ObjectURN>
      <LWM2MVersion>1.2</LWM2MVersion>
      <ObjectVersion>1.2</ObjectVersion>
      <MultipleInstances>Multiple</MultipleInstances>
      <Mandatory>Mandatory</Mandatory>
      <Resources> </Resources>
</Object>

Now:

export const _1 = Type.Object(
  {
    Name: Type.String({ example: "LwM2M Server" }),
    ObjectURN: Type.String({ example: "urn:oma:lwm2m:oma:1:1.2" }),
    LWM2MVersion: Type.Number({ example: 1.2 }),
    ObjectVersion: Type.Number({ example: 1.2 }),
    Resources: Type.Object({...}),
    }),
  },
  {
    description:
      "...",
  }
);

Previously:

export const LwM2M_Server = Type.Object(
  {
    $id: Type.String({ example: 1 }),
    Resources: Type.Object({...}),
    }),
  },
  {
    description:
      "...",
  }
);

Something important to notice is that names can not be the object id only because numbers can not be the name of a variable, so and underscore is added as prefix.

commit: 2ab8d91d0148e868ccc1eaca6a176c7aae6f525e

coderbyheart commented 1 year ago

How about using this notation, this way keys will be unique, but still contain the name for easier reference by developers:

export namespace LwM2M {
    export namespace Object_1 {
        export type LwM2M_Server = {
            Name: string
        }
    }
}

const o1: LwM2M.Object_1.LwM2M_Server = {
    Name: 'foo'
}