How to use @azure-tools/codegen - 10 common examples

To help you get started, we’ve selected a few @azure-tools/codegen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / autorest.go / src / generator / protocol / models.ts View on Github external
function generateEnums(enums?: ChoiceSchema[]): string {
  let text = '';
  for (const enm of values(enums)) {
    if (HasDescription(enm.language.go!)) {
      text += `${comment(enm.language.go!.name, '// ')} - ${enm.language.go!.description}\n`;
    }
    text += `type ${enm.language.go!.name} ${enm.choiceType.language.go!.name}\n\n`;
    enm.choices.sort((a: ChoiceValue, b: ChoiceValue) => { return SortAscending(a.language.go!.name, b.language.go!.name); });
    const vals = new Array();
    text += 'const (\n'
    for (const val of values(enm.choices)) {
      if (HasDescription(val.language.go!)) {
        text += `\t${comment(val.language.go!.name, '// ')} - ${val.language.go!.description}\n`;
      }
      text += `\t${val.language.go!.name} ${enm.language.go!.name} = "${val.value}"\n`;
      vals.push(val.language.go!.name);
    }
    text += ")\n\n"
    text += `func ${enm.language.go!.possibleValuesFunc}() []${enm.language.go!.name} {\n`;
    text += `\treturn []${enm.language.go!.name}{${joinComma(vals, (item: string) => item)}}\n`;
    text += '}\n\n';
github Azure / autorest.go / src / generator / protocol / models.ts View on Github external
text(): string {
    let text = '';
    if (HasDescription(this.Language)) {
      text += `${comment(this.Language.description, '// ')}\n`;
    }
    text += `type ${this.Language.name} struct {\n`;
    for (const prop of values(this.Properties)) {
      if (HasDescription(prop.language.go!)) {
        text += `\t${comment(prop.language.go!.description, '// ')}\n`;
      }
      text += `\t${prop.language.go!.name} ${prop.schema.language.go!.name}\n`;
    }
    text += '}\n\n';
    if (this.Language.errorType) {
      text += `func new${this.Language.name}(resp *azcore.Response) error {\n`;
      text += `\terr := ${this.Language.name}{}\n`;
      text += `\tif err := resp.UnmarshalAsJSON(&err); err != nil {\n`;
      text += `\t\treturn err\n`;
      text += `\t}\n`;
      text += '\treturn err\n';
github Azure / autorest.go / src / generator / protocol / models.ts View on Github external
function generateEnums(enums?: ChoiceSchema[]): string {
  let text = '';
  for (const enm of values(enums)) {
    if (HasDescription(enm.language.go!)) {
      text += `${comment(enm.language.go!.name, '// ')} - ${enm.language.go!.description}\n`;
    }
    text += `type ${enm.language.go!.name} ${enm.choiceType.language.go!.name}\n\n`;
    enm.choices.sort((a: ChoiceValue, b: ChoiceValue) => { return SortAscending(a.language.go!.name, b.language.go!.name); });
    const vals = new Array();
    text += 'const (\n'
    for (const val of values(enm.choices)) {
      if (HasDescription(val.language.go!)) {
        text += `\t${comment(val.language.go!.name, '// ')} - ${val.language.go!.description}\n`;
      }
      text += `\t${val.language.go!.name} ${enm.language.go!.name} = "${val.value}"\n`;
      vals.push(val.language.go!.name);
    }
    text += ")\n\n"
    text += `func ${enm.language.go!.possibleValuesFunc}() []${enm.language.go!.name} {\n`;
    text += `\treturn []${enm.language.go!.name}{${joinComma(vals, (item: string) => item)}}\n`;
    text += '}\n\n';
  }
  return text;
}
github Azure / autorest.go / src / generator / enums.ts View on Github external
export async function generateEnums(session: Session): Promise {
  const enums = getEnums(session.model.schemas);
  if (enums.length === 0) {
    // no enums to generate
    return '';
  }
  let text = await contentPreamble(session);
  for (const enm of values(enums)) {
    if (enm.desc) {
      text += `${comment(enm.name, '/// ')} - ${enm.desc}\n`;
    }
    text += `public enum ${enm.name}: ${enm.type} {\n`;
    const vals = new Array();
    for (const val of values(enm.choices)) {
      if (hasDescription(val.language.go!)) {
        text += `\t${comment(val.language.go!.name, '// ')} - ${val.language.go!.description}\n`;
      }
      text += `\tcase ${val.language.go!.name} = "${val.value}"\n`;
      vals.push(val.language.go!.name);
    }
    // Add any enum funcs
    // text += `func ${enm.funcName}() []${enm.name} {\n`;
    // text += `\treturn []${enm.name}{\t\n`;
    // for (const val of values(vals)) {
    //   text += `\t\t${val},\n`;
    // }
github Azure / autorest.go / src / generator / enums.ts View on Github external
export async function generateEnums(session: Session): Promise {
  const enums = getEnums(session.model.schemas);
  if (enums.length === 0) {
    // no enums to generate
    return '';
  }
  let text = await contentPreamble(session);
  for (const enm of values(enums)) {
    if (enm.desc) {
      text += `${comment(enm.name, '/// ')} - ${enm.desc}\n`;
    }
    text += `public enum ${enm.name}: ${enm.type} {\n`;
    const vals = new Array();
    for (const val of values(enm.choices)) {
      if (hasDescription(val.language.go!)) {
        text += `\t${comment(val.language.go!.name, '// ')} - ${val.language.go!.description}\n`;
      }
      text += `\tcase ${val.language.go!.name} = "${val.value}"\n`;
      vals.push(val.language.go!.name);
    }
    // Add any enum funcs
    // text += `func ${enm.funcName}() []${enm.name} {\n`;
    // text += `\treturn []${enm.name}{\t\n`;
    // for (const val of values(vals)) {
    //   text += `\t\t${val},\n`;
    // }
    // text += '\t}\n';
    // text += '}\n\n';
    // text += `func (c ${enm.name}) ToPtr() *${enm.name} {\n`;
    // text += '\treturn &c\n';
    text += '}\n\n';
  }
github Azure / autorest.go / src / generator / protocol / models.ts View on Github external
text += `${comment(enm.language.go!.name, '// ')} - ${enm.language.go!.description}\n`;
    }
    text += `type ${enm.language.go!.name} ${enm.choiceType.language.go!.name}\n\n`;
    enm.choices.sort((a: ChoiceValue, b: ChoiceValue) => { return SortAscending(a.language.go!.name, b.language.go!.name); });
    const vals = new Array();
    text += 'const (\n'
    for (const val of values(enm.choices)) {
      if (HasDescription(val.language.go!)) {
        text += `\t${comment(val.language.go!.name, '// ')} - ${val.language.go!.description}\n`;
      }
      text += `\t${val.language.go!.name} ${enm.language.go!.name} = "${val.value}"\n`;
      vals.push(val.language.go!.name);
    }
    text += ")\n\n"
    text += `func ${enm.language.go!.possibleValuesFunc}() []${enm.language.go!.name} {\n`;
    text += `\treturn []${enm.language.go!.name}{${joinComma(vals, (item: string) => item)}}\n`;
    text += '}\n\n';
  }
  return text;
}
github Azure / autorest.go / src / generator / protocol / generator.ts View on Github external
export async function protocolGen(host: Host) {
  const debug = await host.GetValue('debug') || false;

  try {

    // get the code model from the core
    const session = await startSession(host, codeModelSchema);
    const namespace = await session.getValue('namespace');

    const operations = await generateOperations(session);

    // output the model to the pipeline.  this must happen after all model
    // updates are complete and before any source files are written.
    host.WriteFile('code-model-v4.yaml', serialize(session.model), undefined, 'code-model-v4');

    for (const op of values(operations)) {
      host.WriteFile(`internal/${namespace}/${op.name.toLowerCase()}.go`, op.content, undefined, 'source-file-go');
    }

    const enums = await generateEnums(session);
    if (enums.length > 0) {
      host.WriteFile(`internal/${namespace}/enums.go`, enums, undefined, 'source-file-go');
    }

    const models = await generateModels(session);
    host.WriteFile(`internal/${namespace}/models.go`, models, undefined, 'source-file-go');

    const timeHelpers = await generateTimeHelpers(session);
    for (const helper of values(timeHelpers)) {
      host.WriteFile(`internal/${namespace}/${helper.name.toLowerCase()}.go`, helper.content, undefined, 'source-file-go');
github Azure / autorest.go / src / generator / generator.ts View on Github external
export async function protocolGen(host: Host) {
  const debug = await host.GetValue('debug') || false;

  try {
    // get the code model from the core
    const session = await startSession(host, codeModelSchema);
    const operations = await generateOperations(session);
    let filePrefix = await session.getValue('file-prefix', '');
    // if a file prefix was specified, ensure it's properly snaked
    if (filePrefix.length > 0 && filePrefix[filePrefix.length - 1] !== '_') {
      filePrefix += '_';
    }

    // output the model to the pipeline.  this must happen after all model
    // updates are complete and before any source files are written.
    host.WriteFile('code-model-v4.yaml', serialize(session.model), undefined, 'code-model-v4');

    for (const op of values(operations)) {
      host.WriteFile(`${filePrefix}${op.name.toLowerCase()}.swift`, op.content, undefined, 'source-file-swift');
    }

    const enums = await generateEnums(session);
    if (enums.length > 0) {
      host.WriteFile(`${filePrefix}enums.swift`, enums, undefined, 'source-file-swift');
    }

    const models = await generateModels(session);
    host.WriteFile(`${filePrefix}models.swift`, models, undefined, 'source-file-swift');

    const client = await generateClient(session);
    host.WriteFile(`${filePrefix}client.swift`, client, undefined, 'source-file-swift');
github Azure / autorest.go / src / generator / convenience / client.ts View on Github external
text += 'func DefaultClientOptions() ClientOptions {\n';
  text += '\treturn ClientOptions{\n';
  text += '\t\tHTTPClient: azcore.DefaultHTTPClientTransport(),\n';
  text += '\t\tRetry: azcore.DefaultRetryOptions(),\n';
  text += '\t}\n';
  text += '}\n\n';

  // Client
  if (session.model.info.description) {
    text += `// Client - ${session.model.info.description}\n`;
  }
  text += 'type Client struct {\n';
  text += `\t${urlVar} *url.URL\n`;
  text += `\t${pipelineVar} azcore.Pipeline\n`;
  for (const op of values(session.model.operationGroups)) {
    text += `\t${camelCase(op.language.go!.clientName)} ${op.language.go!.clientName}\n`;
  }
  text += '}\n\n';

  const endpoint = getDefaultEndpoint(session.model.globalParameters);
  if (endpoint) {
    text += '// DefaultEndpoint is the default service endpoint.\n';
    text += `const DefaultEndpoint = "${endpoint}"\n\n`;
    text += '// NewDefaultClient creates an instance of the Client type using the DefaultEndpoint.\n';
    text += 'func NewDefaultClient(options *ClientOptions) (*Client, error) {\n';
    text += '\treturn NewClient(DefaultEndpoint, options)\n';
    text += '}\n\n';
  }

  text += '// NewClient creates an instance of the Client type with the specified endpoint.\n';
  text += 'func NewClient(endpoint string, options *ClientOptions) (*Client, error) {\n';
  text += '\tif options == nil {\n';
github Azure / autorest.typescript / test / unit / transforms / operationTransforms.spec.ts View on Github external
const getErrorResponseSchema = () => {
        const schema = errorSchema;
        const response = new SchemaResponse(schema);

        response.protocol = {
          http: {
            knownMediaType: KnownMediaType.Json,
            statusCodes: ["default"],
            mediaTypes: "application/json"
          } as any
        };

        return response;
      };
      const get200ResponseSchema = (schema: Schema) => {