Unverified Commit 0d0c4304 authored by Hong Minhee's avatar Hong Minhee
Browse files

Warning logger for vocab objects

parent 385929d4
Loading
Loading
Loading
Loading
+3246 −414

File changed.

Preview size limit exceeded, changes collapsed.

+13 −0
Original line number Diff line number Diff line
@@ -51,6 +51,11 @@ async function* generateClass(
    readonly #documentLoader?: DocumentLoader;
    readonly #contextLoader?: DocumentLoader;
    readonly #tracerProvider?: TracerProvider;
    readonly #warning?: {
      category: string[];
      message: string;
      values?: Record<string, unknown>;
    };
    #cachedJsonLd?: unknown;
    readonly id: URL | null;

@@ -66,6 +71,14 @@ async function* generateClass(
        return this.#tracerProvider;
    }

    protected get _warning(): {
        category: string[];
        message: string;
        values?: Record<string, unknown>;
      } | undefined {
      return this.#warning;
    }

    protected get _cachedJsonLd(): unknown | undefined {
      return this.#cachedJsonLd;
    }
+20 −9
Original line number Diff line number Diff line
@@ -84,11 +84,7 @@ export async function* generateConstructor(
  `;
  for await (const code of generateParametersType(typeUri, types)) yield code;
  yield `,
    {
      documentLoader,
      contextLoader,
      tracerProvider,
    }: {
    options: {
      documentLoader?: DocumentLoader,
      contextLoader?: DocumentLoader,
      tracerProvider?: TracerProvider,
@@ -97,9 +93,16 @@ export async function* generateConstructor(
  `;
  if (type.extends == null) {
    yield `
    this.#documentLoader = documentLoader;
    this.#contextLoader = contextLoader;
    this.#tracerProvider = tracerProvider;
    this.#documentLoader = options.documentLoader;
    this.#contextLoader = options.contextLoader;
    this.#tracerProvider = options.tracerProvider;
    if ("$warning" in options) {
      this.#warning = options.$warning as unknown as {
        category: string[];
        message: string;
        values?: Record<string, unknown>;
      };
    }
    if (values.id == null || values.id instanceof URL) {
      this.id = values.id ?? null;
    } else {
@@ -107,7 +110,7 @@ export async function* generateConstructor(
    }
    `;
  } else {
    yield "super(values, { documentLoader, contextLoader, tracerProvider });";
    yield "super(values, options);";
  }
  for (const property of type.properties) {
    const fieldName = await getFieldName(property.uri);
@@ -203,6 +206,14 @@ export async function* generateCloner(
      contextLoader?: DocumentLoader,
    } = {}
  ): ${type.name} {
    if (this._warning != null) {
      getLogger(this._warning.category).warn(
        this._warning.message,
        this._warning.values
      );
      // @ts-ignore: $warning is not recognized as a property, but it is.
      options = { ...options, $warning: this._warning };
    }
  `;
  if (type.extends == null) {
    yield `
+30 −0
Original line number Diff line number Diff line
@@ -34,6 +34,12 @@ async function* generateProperty(
      yield `${override} get ${property.singularName}(): (${
        getTypeNames(property.range, types)
      } | null) {
        if (this._warning != null) {
          getLogger(this._warning.category).warn(
            this._warning.message,
            this._warning.values
          );
        }
        if (this.${await getFieldName(property.uri)}.length < 1) return null;
        return this.${await getFieldName(property.uri)}[0];
      }
@@ -163,6 +169,12 @@ async function* generateProperty(
       * but returns its \`@id\` URL instead of the object itself.
       */
      ${override} get ${property.singularName}Id(): URL | null {
        if (this._warning != null) {
          getLogger(this._warning.category).warn(
            this._warning.message,
            this._warning.values
          );
        }
        if (this.${await getFieldName(property.uri)}.length < 1) return null;
        const v = this.${await getFieldName(property.uri)}[0];
        if (v instanceof URL) return v;
@@ -179,6 +191,12 @@ async function* generateProperty(
          tracerProvider?: TracerProvider,
        } = {}
      ): Promise<${getTypeNames(property.range, types)} | null> {
        if (this._warning != null) {
          getLogger(this._warning.category).warn(
            this._warning.message,
            this._warning.values
          );
        }
        if (this.${await getFieldName(property.uri)}.length < 1) return null;
        const v = this.${await getFieldName(property.uri)}[0];
        if (v instanceof URL) {
@@ -220,6 +238,12 @@ async function* generateProperty(
       * but returns their \`@id\`s instead of the objects themselves.
       */
      ${override} get ${property.singularName}Ids(): URL[] {
        if (this._warning != null) {
          getLogger(this._warning.category).warn(
            this._warning.message,
            this._warning.values
          );
        }
        return this.${await getFieldName(property.uri)}.map((v) =>
          v instanceof URL ? v : v.id!
        ).filter(id => id !== null);
@@ -235,6 +259,12 @@ async function* generateProperty(
          tracerProvider?: TracerProvider,
        } = {}
      ): AsyncIterable<${getTypeNames(property.range, types)}> {
        if (this._warning != null) {
          getLogger(this._warning.category).warn(
            this._warning.message,
            this._warning.values
          );
        }
        const vs = this.${await getFieldName(property.uri)};
        for (let i = 0; i < vs.length; i++) {
          const v = vs[i];