<commit_before>import {ControlCenter, NotificationCenter, Identifier, VersionedObject, VersionedObjectConstructor, VersionedObjectManager, DataSource, InMemoryDataSource} from './core';

export namespace Transport {

export interface Encoder {
  encode(s: any): any;
}
export interface FlatEncoder extends Encoder {
  encode(s: any, keepAttributes?: boolean): any;
  cc: ControlCenter;
  encodedVo: Set<VersionedObject>;
}
export interface FlatDecoder extends Decoder {
  cc: ControlCenter;
  ccAllowed: Set<VersionedObject> | undefined;
  component: {};
}
export interface Decoder {
  decode(s: any): any;
}
export interface ObjectCoding<I, O, E extends Encoder, D extends Decoder> {
  is: string | undefined,
  canEncode(e: E, s) : s is I,
  encode(e: E, s: I): O,
  canDecode(d: D, s) : s is O,
  decode(d: D, s: O): I,
};
export type ObjectFlatCoding<I, O> = ObjectCoding<I, O, FlatEncoder, FlatDecoder>;

const jsonEncoders: ObjectCoding<any, any, FlatEncoder, FlatDecoder>[]= [
  { is: "Map",
    canEncode(e, s) { return s instanceof Map },
    encode(e, s) {
      let r: any = { __is__: "Map", entries: [] };
      for (let [k, v] of s)
        r.entries.push([e.encode(k), e.encode(v)]);
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let r = new Map();
      for (let [k, v] of s.entries)
        r.set(d.decode(k), d.decode(v));
      return r;
    },
  } as ObjectFlatCoding<Map<any, any>, { __is__: "Map", entries: [any, any][] }>,
  { is: "Set",
    canEncode(e, s) { return s instanceof Set },
    encode(e, s) {
      let r: any = { __is__: "Set", entries: [] };
      for (let v of s)
        r.entries.push(e.encode(v));
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let r = new Set();
      for (let v of s.entries)
        r.add(d.decode(v));
      return r;
    },
  } as ObjectFlatCoding<Set<any>, { __is__: "Set", entries: any[] }>,
  { is: "VersionedObject",
    canEncode(e, s) { return s instanceof VersionedObject },
    encode(e, s) {
      let r: any;
      let m = s.manager();
      m = (m as any)._manager || m; // bypass UnregisteredVersionedObjectManager
      let id = m.id()
      if (VersionedObjectManager.isLocalId(id))
        throw new Error(`reference to locally defined object ${id}`);
      r = { __is__: "VersionedObject", is: m.name(), id: m.id() };
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let id = s.id;
      if (VersionedObjectManager.isLocalId(id))
        throw new Error(`reference to locally defined object ${id}`);
      let vo = d.cc.find(id);
      if (!vo) {
        vo = d.cc.create(s.is);
        vo.manager().setId(s.id);
        if (d.ccAllowed)
          d.ccAllowed.add(vo);
      }
      else if (d.ccAllowed && !d.ccAllowed.has(vo))
        throw new Error(`reference to reserved cc object ${id}`);
      return vo;
    },
  } as ObjectFlatCoding<VersionedObject, { __is__: "VersionedObject", is: string, id: Identifier }>,<commit_msg>core: add support for date encoding<commit_after>import {ControlCenter, NotificationCenter, Identifier, VersionedObject, VersionedObjectConstructor, VersionedObjectManager, DataSource, InMemoryDataSource} from './core';

export namespace Transport {

export interface Encoder {
  encode(s: any): any;
}
export interface FlatEncoder extends Encoder {
  encode(s: any, keepAttributes?: boolean): any;
  cc: ControlCenter;
  encodedVo: Set<VersionedObject>;
}
export interface FlatDecoder extends Decoder {
  cc: ControlCenter;
  ccAllowed: Set<VersionedObject> | undefined;
  component: {};
}
export interface Decoder {
  decode(s: any): any;
}
export interface ObjectCoding<I, O, E extends Encoder, D extends Decoder> {
  is: string | undefined,
  canEncode(e: E, s) : s is I,
  encode(e: E, s: I): O,
  canDecode(d: D, s) : s is O,
  decode(d: D, s: O): I,
};
export type ObjectFlatCoding<I, O> = ObjectCoding<I, O, FlatEncoder, FlatDecoder>;

const jsonEncoders: ObjectCoding<any, any, FlatEncoder, FlatDecoder>[]= [
  { is: "Map",
    canEncode(e, s) { return s instanceof Map },
    encode(e, s) {
      let r: any = { __is__: "Map", entries: [] };
      for (let [k, v] of s)
        r.entries.push([e.encode(k), e.encode(v)]);
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let r = new Map();
      for (let [k, v] of s.entries)
        r.set(d.decode(k), d.decode(v));
      return r;
    },
  } as ObjectFlatCoding<Map<any, any>, { __is__: "Map", entries: [any, any][] }>,
  { is: "Set",
    canEncode(e, s) { return s instanceof Set },
    encode(e, s) {
      let r: any = { __is__: "Set", entries: [] };
      for (let v of s)
        r.entries.push(e.encode(v));
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let r = new Set();
      for (let v of s.entries)
        r.add(d.decode(v));
      return r;
    },
  } as ObjectFlatCoding<Set<any>, { __is__: "Set", entries: any[] }>,
  { is: "Date",
    canEncode(e, s) { return s instanceof Date; },
    canDecode(d, s) { return s && s.__is__ === this.is; },
    encode(e, s) {
      return { __is__: this.is, v: s.getTime() };
    },
    decode(d, s) {
      return new Date(s.v);
    },
  } as ObjectFlatCoding<Date, { __is__: "Date", v: number }>,
  { is: "VersionedObject",
    canEncode(e, s) { return s instanceof VersionedObject },
    encode(e, s) {
      let r: any;
      let m = s.manager();
      m = (m as any)._manager || m; // bypass UnregisteredVersionedObjectManager
      let id = m.id()
      if (VersionedObjectManager.isLocalId(id))
        throw new Error(`reference to locally defined object ${id}`);
      r = { __is__: "VersionedObject", is: m.name(), id: m.id() };
      return r;
    },
    canDecode(d, s) { return s && s.__is__ === this.is },
    decode(d, s) {
      let id = s.id;
      if (VersionedObjectManager.isLocalId(id))
        throw new Error(`reference to locally defined object ${id}`);
      let vo = d.cc.find(id);
      if (!vo) {
        vo = d.cc.create(s.is);
        vo.manager().setId(s.id);
        if (d.ccAllowed)
          d.ccAllowed.add(vo);
      }
      else if (d.ccAllowed && !d.ccAllowed.has(vo))
        throw new Error(`reference to reserved cc object ${id}`);
      return vo;
    },
  } as ObjectFlatCoding<VersionedObject, { __is__: "VersionedObject", is: string, id: Identifier }>,
{'fraction_non_alphanumeric': 0.15985872235872237, 'fraction_numerical': 0.0, 'mean_word_length': 2.8652818991097924, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '951015', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2278, 'n_words': 647}