Giter Site home page Giter Site logo

swaggest / go-code-builder Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 750 KB

JSON Schema enabled Go code building abstraction for PHP

License: MIT License

PHP 20.76% Go 79.15% Makefile 0.09%
go php-library json-schema json-schema-generator code-generator

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::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.

x-generate

A boolean value to control whether property should be added to generated struct. Property with "x-generate": false will be skipped. If GoBuilder option requireXGenerate is set to true only properties with "x-generate": true will be generated.

CLI Tool

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

go-code-builder's People

Contributors

cussrox avatar vearutop avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

cussrox

go-code-builder's Issues

OOM trying to generate go from AsyncAPI schema

I ran into this error using json-cli - but the error points me to this repository.

To reproduce, run the following

json-cli gen-go https://raw.githubusercontent.com/asyncapi/bindings/master/http/json_schemas/operation.json

And you should see

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 266240 bytes) in phar:///usr/local/go/bin/json-cli/vendor/swaggest/go-code-builder/src/GoCodeBuilder.php on line 61

I'm trying to generate an HTTP binding that I can contribute to your go-asyncapi project. I've tried upping the memory limit all the way to 2GB and it still fails. I'm trying to find a fix myself, but I have never even set up a PHP development environment, so if you have any advice or fixes they would be welcome.

For what it's worth, this fails in that repo as well running make gen-2.0.0 after adding the file referenced above as http-operation-binding-object-0.1.0.json and then referencing it in asyncapi-2.0.0.json.

Changes can be seen here

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.