elmgen

package
v0.0.0-...-f1df956 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

This file is part of protoc-gen-elmer.

Protoc-gen-elmer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Protoc-gen-elmer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Protoc-gen-elmer. If not, see <https://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatFile

func FormatFile(plugin *protogen.Plugin, path string, file *protogen.GeneratedFile) *protogen.GeneratedFile

Formats our Elm code and returns the replacement File. Errors are passed to plugin.Error. Assumes `elm-format` is in `$PATH`

func GenerateCodec

func GenerateCodec(m *Module, g *protogen.GeneratedFile) bool

Generates Elm decoders and encoders (making a codec) to a file

func GenerateFuzzTests

func GenerateFuzzTests(m *Module, g *protogen.GeneratedFile) bool

func GenerateTwirp

func GenerateTwirp(m *Module, g *protogen.GeneratedFile) bool

Types

type CommentSet

type CommentSet struct {
	LeadingDetached []Comments
	Leading         Comments
	Trailing        Comments
}

Describes a set of comments from the Protobuf source

type Comments

type Comments string

Specialised Elm comments. Has a stringer method that wraps itself in dash comments (--)

func (Comments) String

func (c Comments) String() (block string)

Formats an Elm comment by trimming white space and prefixing it with double dashes (--). String may contain new lines.

type ElmRef

type ElmRef struct {
	Module, ID string
}

Elm reference pointing an identifier e.g., a type or function in another module. Module is blank for local references.

func (*ElmRef) String

func (r *ElmRef) String() string

Converts an Elm reference to Elm code. If local, drops the module.

type ElmType

type ElmType struct {
	*ElmRef
	Zero, Decoder, Encoder, Fuzzer *ElmRef
}

Speciality reference for a codegen type that has our derived functions. Refs are never nil. The fuzzer assumes a reference to another module with the "Tests" suffix (see `NewModule`)

type Field

type Field struct {
	Label    string
	Desc     protoreflect.FieldDescriptor
	Oneof    *Oneof
	Comments *CommentSet
}

A record field. Desc may be nil if it's a non-synthetic Oneof.

type Module

type Module struct {
	ProtoPackage string
	Name, Path   string
	Imports      []string

	Unions   Unions
	Oneofs   Oneofs
	Records  Records
	Services Services
	// contains filtered or unexported fields
}

Top-level structures describing an Elm Module

func NewModule

func NewModule(suffix string, input *ProtoPackage) *Module

Entry point for elmgen. Builds an Elm module from a given proto File. The module name may be suffixed to allow for different derivative use cases e.g., a codec with no suffix and the suffix "Twirp" for a client could live alongside each other.

func (*Module) NewElmType

func (m *Module) NewElmType(p packager, d fullNamer) *ElmType

Creates a new Elm type reference (uppercase first char) from a proto ident

func (*Module) NewElmValue

func (m *Module) NewElmValue(p packager, prefix string, d fullNamer) *ElmRef

Creates a new Elm value (lowercase first char) from a proto ident. Prefix must be non-empty

type Oneof

type Oneof struct {
	Type        *ElmType
	IsSynthetic bool
	Variants    []*OneofVariant
	Comments    *CommentSet
}

Oneof is a sum type, similar to a Union, whose tags hold a reference to another data type. The data type can be complex (another type like a record or union) or simple (scalar data like a bool). Oneofs are used in records where a field should hold "one and only one" from a set of types. If IsSynthetic is set then it's an optional field with a single choice for its data type which can be present or missing.

type OneofVariant

type OneofVariant struct {
	ID    *ElmRef // Promoted Field label
	Field *Field
}

Describes a Oneof option. Very similar to Variant except it holds a data type as well.

type Oneofs

type Oneofs []*Oneof

Oneofs sortable by ID

func (Oneofs) Len

func (a Oneofs) Len() int

func (Oneofs) Less

func (a Oneofs) Less(i, j int) bool

func (Oneofs) Swap

func (a Oneofs) Swap(i, j int)

type ProtoPackage

type ProtoPackage struct {
	Name     protoreflect.FullName
	Generate bool

	Enums      []*protogen.Enum
	Messages   []*protogen.Message
	Extensions []*protogen.Extension
	Services   []*protogen.Service
}

Describes our PB inputs, possibly from multiple files

func FilesToPackages

func FilesToPackages(files []*protogen.File) []*ProtoPackage

Protobuf packages can be made up of multiple files. Merge them into their own packages

type RPC

type RPC struct {
	ID      *ElmRef
	In, Out *ElmType

	InStreaming, OutStreaming bool

	Service  protoreflect.FullName
	Method   protoreflect.Name
	Comments *CommentSet
}

Describes an RPC method

type RPCs

type RPCs []*RPC

RPC sortable by ID

func (RPCs) Len

func (a RPCs) Len() int

func (RPCs) Less

func (a RPCs) Less(i, j int) bool

func (RPCs) Swap

func (a RPCs) Swap(i, j int)

type Record

type Record struct {
	Type     *ElmType
	Fields   []*Field
	Comments *CommentSet
}

A record is derived from a Protobuf message

func (*Record) Oneofs

func (r *Record) Oneofs() (fields []*Field)

type Records

type Records []*Record

Records sortable by ID

func (Records) Len

func (a Records) Len() int

func (Records) Less

func (a Records) Less(i, j int) bool

func (Records) Swap

func (a Records) Swap(i, j int)

type Service

type Service struct {
	Label    string
	Methods  RPCs
	Comments *CommentSet
}

Represents a grouping of RPC methods. Not necessarily important but used to retain comments

type Services

type Services []*Service

Services sortable by ID

func (Services) Len

func (a Services) Len() int

func (Services) Less

func (a Services) Less(i, j int) bool

func (Services) Swap

func (a Services) Swap(i, j int)

type Union

type Union struct {
	Type     *ElmType
	Variants []*Variant
	Aliases  []*VariantAlias
	Comments *CommentSet
}

Union is a sum type with simple tags that won't hold data. Always holds at least one variant with the first being the default. Derived from Protobuf enums.

func (*Union) Default

func (u *Union) Default() *Variant

Returns a union's default variant. Never returns nil. All unions have at least one variant. The first is our default (zero)

type Unions

type Unions []*Union

Unions sortable by ID

func (Unions) Len

func (a Unions) Len() int

func (Unions) Less

func (a Unions) Less(i, j int) bool

func (Unions) Swap

func (a Unions) Swap(i, j int)

type Variant

type Variant struct {
	ID       *ElmRef
	Label    string
	Number   protoreflect.EnumNumber
	Comments *CommentSet
}

Describes a Union tag

type VariantAlias

type VariantAlias struct {
	Alias    *ElmRef
	Variant  *Variant
	Comments *CommentSet
}

VariantAlias is a Variant with an alternative name and the same wire number. First Variant seen is the real one, subsequent are alternate names.

Jump to

Keyboard shortcuts

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