<commit_before>import yaml from 'js-yaml'
import {
  CLOUDFORMATION_SCHEMA,
  cloudformationTags as CFTags
} from 'cloudformation-js-yaml-schema'


export default class LenticularYamlDoc {
  constructor (documentString, config={}) {
    this.data = yaml.safeLoad(documentString, {schema: LENTICULAR_SCHEMA})
    this.config = config
  }

  toCloudFormationYamlString () {
    const yamlData = lenticularYamlDataToCloudFormationYamlData(this.data, this.config)
    return yamlDataToString(yamlData, CLOUDFORMATION_SCHEMA)
  }

  toYamlString (schema = LENTICULAR_SCHEMA) {
    return yamlDataToString(this.data, LENTICULAR_SCHEMA)
  }
}


function yamlDataToString (data, schema) {
  return yaml.safeDump(data, {schema})
}


function lenticularYamlDataToCloudFormationYamlData (data, config) {
  if (data.constructor === Object) {
    const copy = {}
    Object.keys(data).forEach(key => {
      copy[key] = lenticularYamlDataToCloudFormationYamlData(data[key], config)
    })
    return copy
  }
  else if (Array.isArray(data)) {
    return data.map(val => lenticularYamlDataToCloudFormationYamlData(val, config))
  }
  else if (data instanceof LenticularYamlType) {
    return data.toCloudFormationYamlData(config)
  }
  else {
    return data
  }
}

class LenticularYamlType {}

class ResourceName extends LenticularYamlType {
  constructor (name) {
    super()
    this.name = name
  }

  toCloudFormationYamlData (config) {
    return new (cfClasses.get('!Join:sequence'))(['-', [
      new (cfClasses.get('!Ref:scalar'))('AWS::StackName'), this.name
    ]])
  }
}

class ResourceNameWithRegion extends ResourceName {
  toCloudFormationYamlData (config) {
    return new (cfClasses.get('!Join:sequence'))(['-', [
      new (cfClasses.get('!Ref:scalar'))('AWS::StackName'),
      this.name,
      new (cfClasses.get('!Ref:scalar'))('AWS::Region')
    ]])
  }
}

class ProductName extends LenticularYamlType {
  toCloudFormationYamlData (config) {
    return config.productName
  }
}


const ResourceNameYamlType = new yaml.Type('!Lenticular::ResourceName', {
  kind: 'scalar',
  instanceOf: ResourceName,
  construct: data => new ResourceName(data),
  represent: (ref, style) => ref.name
})

const ResourceNameWithRegionYamlType = new yaml.Type('!Lenticular::ResourceNameWithRegion', {
  kind: 'scalar',
  instanceOf: ResourceNameWithRegion,
  construct: data => new ResourceNameWithRegion(data),
  represent: (ref, style) => ref.name
})

const ProductNameYamlType = new yaml.Type('!Lenticular::ProductName', {
  kind: 'scalar',
  instanceOf: ProductName,
  construct: data => new ProductName(),
  represent: (ref, style) => ref.name
})

const cfClasses = new Map(
  CFTags.map(tag => [`${tag.tag}:${tag.kind}`, tag.construct])
)

const LENTICULAR_SCHEMA = yaml.Schema.create(CFTags.concat([
  ResourceNameYamlType, ResourceNameWithRegionYamlType, ProductNameYamlType
]))<commit_msg>Export yaml schema for the heck of it<commit_after>import yaml from 'js-yaml'
import {
  CLOUDFORMATION_SCHEMA,
  cloudformationTags as CFTags
} from 'cloudformation-js-yaml-schema'


export default class LenticularYamlDoc {
  constructor (documentString, config={}) {
    this.data = yaml.safeLoad(documentString, {schema: LENTICULAR_SCHEMA})
    this.config = config
  }

  toCloudFormationYamlString () {
    const yamlData = lenticularYamlDataToCloudFormationYamlData(this.data, this.config)
    return yamlDataToString(yamlData, CLOUDFORMATION_SCHEMA)
  }

  toYamlString (schema = LENTICULAR_SCHEMA) {
    return yamlDataToString(this.data, LENTICULAR_SCHEMA)
  }
}


function yamlDataToString (data, schema) {
  return yaml.safeDump(data, {schema})
}


function lenticularYamlDataToCloudFormationYamlData (data, config) {
  if (data.constructor === Object) {
    const copy = {}
    Object.keys(data).forEach(key => {
      copy[key] = lenticularYamlDataToCloudFormationYamlData(data[key], config)
    })
    return copy
  }
  else if (Array.isArray(data)) {
    return data.map(val => lenticularYamlDataToCloudFormationYamlData(val, config))
  }
  else if (data instanceof LenticularYamlType) {
    return data.toCloudFormationYamlData(config)
  }
  else {
    return data
  }
}

class LenticularYamlType {}

class ResourceName extends LenticularYamlType {
  constructor (name) {
    super()
    this.name = name
  }

  toCloudFormationYamlData (config) {
    return new (cfClasses.get('!Join:sequence'))(['-', [
      new (cfClasses.get('!Ref:scalar'))('AWS::StackName'), this.name
    ]])
  }
}

class ResourceNameWithRegion extends ResourceName {
  toCloudFormationYamlData (config) {
    return new (cfClasses.get('!Join:sequence'))(['-', [
      new (cfClasses.get('!Ref:scalar'))('AWS::StackName'),
      this.name,
      new (cfClasses.get('!Ref:scalar'))('AWS::Region')
    ]])
  }
}

class ProductName extends LenticularYamlType {
  toCloudFormationYamlData (config) {
    return config.productName
  }
}


const ResourceNameYamlType = new yaml.Type('!Lenticular::ResourceName', {
  kind: 'scalar',
  instanceOf: ResourceName,
  construct: data => new ResourceName(data),
  represent: (ref, style) => ref.name
})

const ResourceNameWithRegionYamlType = new yaml.Type('!Lenticular::ResourceNameWithRegion', {
  kind: 'scalar',
  instanceOf: ResourceNameWithRegion,
  construct: data => new ResourceNameWithRegion(data),
  represent: (ref, style) => ref.name
})

const ProductNameYamlType = new yaml.Type('!Lenticular::ProductName', {
  kind: 'scalar',
  instanceOf: ProductName,
  construct: data => new ProductName(),
  represent: (ref, style) => ref.name
})

const cfClasses = new Map(
  CFTags.map(tag => [`${tag.tag}:${tag.kind}`, tag.construct])
)

export const LENTICULAR_SCHEMA = yaml.Schema.create(CFTags.concat([
  ResourceNameYamlType, ResourceNameWithRegionYamlType, ProductNameYamlType
]))
{'fraction_non_alphanumeric': 0.1278648974668275, 'fraction_numerical': 0.0, 'mean_word_length': 4.786640079760718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '6173988', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1906, 'n_words': 366}