Giter Club home page Giter Club logo

go-code-builder's Introduction

Swaggest JSON-schema enabled Go code builder

Build Status codecov Code lines Comments

This library generates Go mapping structures defined by JSON schema.

Example

Generated code for schema.

<?php
namespace MyApp;

use Swaggest\GoCodeBuilder\JsonSchema\GoBuilder;
use Swaggest\GoCodeBuilder\JsonSchema\StructHookCallback;
use Swaggest\GoCodeBuilder\Templates\GoFile;
use Swaggest\GoCodeBuilder\Templates\Struct\StructDef;
use Swaggest\JsonSchema\Schema;

require_once __DIR__ . '/vendor/autoload.php';

$schemaData = json_decode(file_get_contents(__DIR__ . '/swagger-schema.json'));
$schema = Schema::import($schemaData);

$builder = new GoBuilder();
$builder->structCreatedHook = new StructHookCallback(function (StructDef $structDef, $path, $schema) use ($builder) {
    if ('#' === $path) {
        $structDef->setName('SwaggerSchema');
    } elseif (0 === strpos($path, '#/definitions/')) {
        $name = $builder->codeBuilder->exportableName(substr($path, strlen('#/definitions/')));
        $structDef->setName($name);
    }
});
$builder->getType($schema);

$goFile = new GoFile('swagger');
foreach ($builder->getGeneratedStructs() as $generatedStruct) {
    $goFile->getCode()->addSnippet($generatedStruct->structDef);
}
$goFile->getCode()->addSnippet($builder->getCode());

API Documentation

Classes documentation.

Schema extensions

Magic properties (vendor extensions) defined in schema enable special handling.

x-go-type

Can have a value string or an object. Contains type reference that can be used instead of generated type.

If $ignoreXGoType option is true value of vendor extension is disregarded and type is generated.

Value examples:

  • "[]myorg.com/go-null-types.NullString::nulltypes.NullString"
  • "myorg.com/my-app/order.Entity"
  • "float64"
  • {"import": {"package": "my-package/domain/orders"}, "type": "Order"}
  • {"import": {"package": "my-package", "alias": "mp"}, "type": "Order"}

x-nullable, nullable

If true schema type is converted to [<type>, "null"]. Requires $enableXNullable option.

x-omitempty

A boolean value to control ,omitempty presence.

CLI Tool

You can use json-cli to generate Go structures from command line.

go-code-builder's People

Contributors

vearutop avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.