Generator: Go code generator

Created on 28 May 2017  路  8Comments  路  Source: asyncapi/generator

It would be awesome to have a code generator for Go. Anyone around who wants to participate?

Most helpful comment

FYI, there is a tool that can generate Go and PHP code for message models.

json-cli gen-go "https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0-rc1/examples/1.2.0/streetlights.yml" \
    --ptr-in-schema "#/components/messages/lightMeasured/payload" "#/components/messages/turnOnOff/payload" \
    --def-ptr "#/components/schemas" \
    --package-name message \
    --output ./entities.go
// Code generated by github.com/swaggest/json-cli v1.6.3, DO NOT EDIT.

// Package message contains JSON mapping structures.
package message

import (
        "fmt"
        "time"
)

// LightMeasuredPayload structure is generated from "#/components/schemas/lightMeasuredPayload".
type LightMeasuredPayload struct {
        Lumens int64      `json:"lumens,omitempty"` // Light intensity measured in lumens.
        SentAt *time.Time `json:"sentAt,omitempty"` // Date and time when the message was sent.
}

// TurnOnOffPayload structure is generated from "#/components/schemas/turnOnOffPayload".
type TurnOnOffPayload struct {
        Command TurnOnOffPayloadCommand `json:"command,omitempty"` // Whether to turn on or off the light.
        SentAt  *time.Time              `json:"sentAt,omitempty"`  // Date and time when the message was sent.
}

// TurnOnOffPayloadCommand is an enum type.
type TurnOnOffPayloadCommand string

// TurnOnOffPayloadCommand values enumeration.
const (
        TurnOnOffPayloadCommandOn = TurnOnOffPayloadCommand("on")
        TurnOnOffPayloadCommandOff = TurnOnOffPayloadCommand("off")
)

// MarshalJSON encodes JSON.
func (i TurnOnOffPayloadCommand) MarshalJSON() ([]byte, error) {
        switch i {
        case TurnOnOffPayloadCommandOn:
        case TurnOnOffPayloadCommandOff:

        default:
                return nil, fmt.Errorf("unexpected TurnOnOffPayloadCommand value: %v", i)
        }

        return json.Marshal(string(i))
}

// UnmarshalJSON decodes JSON.
func (i *TurnOnOffPayloadCommand) UnmarshalJSON(data []byte) error {
        var ii string
        err := json.Unmarshal(data, &ii)
        if err != nil {
                return err
        }
        v := TurnOnOffPayloadCommand(ii)
        switch v {
        case TurnOnOffPayloadCommandOn:
        case TurnOnOffPayloadCommandOff:

        default:
                return fmt.Errorf("unexpected TurnOnOffPayloadCommand value: %v", v)
        }

        *i = v
        return nil
}

All 8 comments

@timburks or colleagues?

I'm working on one now but I definately need some help. At the moment have simpe strcut generation for messages but need to add some tests.

Hey @vgarvardt! I'm planning to work on one too. Join the Slack channel and let's work together on this.

FYI, there is a tool that can generate Go and PHP code for message models.

json-cli gen-go "https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0-rc1/examples/1.2.0/streetlights.yml" \
    --ptr-in-schema "#/components/messages/lightMeasured/payload" "#/components/messages/turnOnOff/payload" \
    --def-ptr "#/components/schemas" \
    --package-name message \
    --output ./entities.go
// Code generated by github.com/swaggest/json-cli v1.6.3, DO NOT EDIT.

// Package message contains JSON mapping structures.
package message

import (
        "fmt"
        "time"
)

// LightMeasuredPayload structure is generated from "#/components/schemas/lightMeasuredPayload".
type LightMeasuredPayload struct {
        Lumens int64      `json:"lumens,omitempty"` // Light intensity measured in lumens.
        SentAt *time.Time `json:"sentAt,omitempty"` // Date and time when the message was sent.
}

// TurnOnOffPayload structure is generated from "#/components/schemas/turnOnOffPayload".
type TurnOnOffPayload struct {
        Command TurnOnOffPayloadCommand `json:"command,omitempty"` // Whether to turn on or off the light.
        SentAt  *time.Time              `json:"sentAt,omitempty"`  // Date and time when the message was sent.
}

// TurnOnOffPayloadCommand is an enum type.
type TurnOnOffPayloadCommand string

// TurnOnOffPayloadCommand values enumeration.
const (
        TurnOnOffPayloadCommandOn = TurnOnOffPayloadCommand("on")
        TurnOnOffPayloadCommandOff = TurnOnOffPayloadCommand("off")
)

// MarshalJSON encodes JSON.
func (i TurnOnOffPayloadCommand) MarshalJSON() ([]byte, error) {
        switch i {
        case TurnOnOffPayloadCommandOn:
        case TurnOnOffPayloadCommandOff:

        default:
                return nil, fmt.Errorf("unexpected TurnOnOffPayloadCommand value: %v", i)
        }

        return json.Marshal(string(i))
}

// UnmarshalJSON decodes JSON.
func (i *TurnOnOffPayloadCommand) UnmarshalJSON(data []byte) error {
        var ii string
        err := json.Unmarshal(data, &ii)
        if err != nil {
                return err
        }
        v := TurnOnOffPayloadCommand(ii)
        switch v {
        case TurnOnOffPayloadCommandOn:
        case TurnOnOffPayloadCommandOff:

        default:
                return fmt.Errorf("unexpected TurnOnOffPayloadCommand value: %v", v)
        }

        *i = v
        return nil
}

Lovely! Thanks for sharing!

@vgarvardt hey man, are you still in the topic? I don't think that @fmvilas is still involved in writing a template but seems more people are interested #209 so would be good to join forces maybe?

This issue has been automatically marked as stale because it has not had recent activity :sleeping:
It will be closed in 30 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions :heart:

Closing this one
Discussions should be under one issue https://github.com/asyncapi/generator/issues/209

Was this page helpful?
0 / 5 - 0 ratings

Related issues

derberg picture derberg  路  8Comments

aravindajju picture aravindajju  路  9Comments

Ellocsys picture Ellocsys  路  9Comments

GodsBoss picture GodsBoss  路  4Comments

fmvilas picture fmvilas  路  9Comments