Unverified Commit 31b0b19c authored by Hong Minhee's avatar Hong Minhee
Browse files

Let proofs embed their own contexts

parent d70ea74c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@
    "spki",
    "subproperty",
    "superproperty",
    "supertypes",
    "tempserver",
    "unfollow",
    "unfollowing",
+1012 −110

File changed.

Preview size limit exceeded, changes collapsed.

+36 −2
Original line number Diff line number Diff line
@@ -88,11 +88,45 @@ export async function* generateEncoder(
        { documentLoader: options.contextLoader },
      );
    }
    return await jsonld.compact(
    const docContext = options.context ??
      ${JSON.stringify(type.defaultContext)};
    const compacted = await jsonld.compact(
      values,
      options.context ?? ${JSON.stringify(type.defaultContext)},
      docContext,
      { documentLoader: options.contextLoader },
    );
    if (docContext != null) {
      // Embed context
  `;
  const supertypes: string[] = [];
  for (
    let uri: string | undefined = typeUri;
    uri != null;
    uri = types[uri].extends
  ) {
    supertypes.push(uri);
  }
  for (const supertype of supertypes) {
    for (const property of types[supertype].properties) {
      if (property.embedContext == null) continue;
      const compactName = property.embedContext.compactName;
      yield `
      if (${JSON.stringify(compactName)} in compacted &&
          compacted.${compactName} != null) {
        if (Array.isArray(compacted.${compactName})) {
          for (const element of compacted.${compactName}) {
            element["@context"] = docContext;
          }
        } else {
         compacted.${compactName}["@context"] = docContext;
        }
      }
      `;
    }
  }
  yield `
    }
    return compacted;
  }
  `;
}
+17 −0
Original line number Diff line number Diff line
@@ -81,6 +81,23 @@ export interface PropertySchemaBase {
   * the generated property accessors.
   */
  description: string;

  /**
   * Whether the enclosed object should have its own context when the document
   * is compacted.
   */
  embedContext?: {
    /**
     * The compact name of the property that contains the context.
     */
    compactName: string;

    /**
     * Whether the embedded context should be the same as the context of
     * the enclosing document.
     */
    inherit: true;
  };
}

export type PropertySchemaTyping = {
+15 −0
Original line number Diff line number Diff line
@@ -72,6 +72,21 @@ $defs:
          The description of the property.  It is used as the doc comment of
          the generated property accessors.
        type: string
      embedContext:
        description: >-
          Whether the enclosed object should have its own context when
          the document is compacted.
        type: object
        properties:
          compactName:
            description: >-
              The compact name of the property that contains the context.
            type: string
          inherit:
            description: >-
              Whether the embedded context should be the same as the context of
              the enclosing document.
            const: true
    required:
    - singularName
    - uri
Loading