ptlbuilder

package module
v0.0.0-...-1a1f2e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 8 Imported by: 0

README

PTLBuilder

PTLBuilder is a Go library for generating protocol code. It simplifies the creation of communication protocols by automatically generating the necessary Go code based on a protocol specification.

Installation

go get github.com/SSam0419/ptlbuilder

Usage

package main

import "github.com/SSam0419/ptlbuilder"

func main() {
    ptlbuilder.Generate(ptlbuilder.ProtocolSpec{
        Package: "protocol",
        Commands: []ptlbuilder.Command{
            {
                Name: "RegisterClient",
                Fields: []ptlbuilder.Field{
                    {Name: "ClientAddr", Type: "string"},
                },
            },
            {
                Name: "ListenTopic",
                Fields: []ptlbuilder.Field{
                    {Name: "Topic", Type: "string"},
                    {Name: "ClientAddr", Type: "string"},
                },
            },
            {
                Name: "SendMessage",
                Fields: []ptlbuilder.Field{
                    {Name: "Topic", Type: "string"},
                    {Name: "ClientAddr", Type: "string"},
                    {Name: "Payload", Type: "[]byte"},
                },
            },
        },
    })
}

Protocol Specification

ProtocolSpec

The main structure for defining your protocol:

type ProtocolSpec struct {
    Package  string    // Target package name
    Commands []Command // List of commands
}
Command

Defines a single command in the protocol:

type Command struct {
    Name   string  // Command name
    Fields []Field // Command fields
}
Field

Defines a field within a command:

type Field struct {
    Name string // Field name
    Type string // Field type (Go type)
}
Generated Code

The library will generate a protocol.go file in the protocol directory

Documentation

Overview

Package ptlbuilder provides utilities for building protocol specifications.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyName    = errors.New("name cannot be empty")
	ErrEmptyType    = errors.New("type cannot be empty")
	ErrEmptyPackage = errors.New("package name cannot be empty")
	ErrZeroTimeout  = errors.New("timeout must be greater than 0")
)

Common errors

Functions

This section is empty.

Types

type Command

type Command struct {
	Name   string
	Fields []Field
}

Command represents a protocol command with its fields.

func NewCommand

func NewCommand(name string) *Command

NewCommand creates a new command with validation. Only allow UpperCamel / Pascal Case

func (*Command) AddField

func (c *Command) AddField(name string, fieldType string) *Command

AddField adds a new field to the command with validation.

type Field

type Field struct {
	Name string
	Type string
}

Field represents a single field in a protocol command.

func NewField

func NewField(name, fieldType string) *Field

NewField creates a new field with validation.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator handles protocol code generation

func NewGenerator

func NewGenerator(spec *ProtocolSpec) *Generator

NewGenerator creates a new protocol generator

func (*Generator) Generate

func (g *Generator) Generate() error

Generate generates protocol code and documentation

type ProtocolSpec

type ProtocolSpec struct {
	Package  string
	Commands []*Command
	Timeout  uint // maximum seconds allowed to decode message
}

ProtocolSpec represents the complete protocol specification.

func NewProtocolSpec

func NewProtocolSpec(pkg string, timeout uint) *ProtocolSpec

NewProtocolSpec creates a new protocol specification with validation.

func (*ProtocolSpec) AddCommand

func (p *ProtocolSpec) AddCommand(cmd *Command) *ProtocolSpec

AddCommand adds a new command to the protocol specification.

func (*ProtocolSpec) Validate

func (p *ProtocolSpec) Validate() error

Validate checks if the protocol specification is valid.

Directories

Path Synopsis
example
Code generated by ptlbuilder;
Code generated by ptlbuilder;

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL