constructor

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Constructor

Provides a quick and dirty way to convert JSON data dumps to a Golang struct.

Usage

Given the following JSON data dump:

{
  "users": [
    {
      "first_name": "Alice",
      "username": "alice",
      "age": "23"
    },
    {
      "username": "bob",
      "age": 42
    }
  ]
}

You can convert it to a Golang struct like this:

package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/0x51-dev/constructor"
	"github.com/0x51-dev/constructor/fmt/gofmt"
)

func main() {
	data, _ := os.ReadFile("data.json")
	n, _ := constructor.Construct(bytes.NewReader(data))
	str, _ := gofmt.NodeToGo("Users", n)
	fmt.Println(str) // Prints out formatted Golang struct.
}

Output:

package users

type Users struct {
	Users []struct {
		Age       any /* json.Number, string */ `json:"age"`
		FirstName *string                       `json:"first_name"`
		Username  string                        `json:"username"`
	} `json:"users"`
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCombinationError

func NewCombinationError(a, b Node) error

Types

type Any

type Any struct{}

func (*Any) Combine

func (a *Any) Combine(n Node) (Node, error)

func (*Any) Equals

func (*Any) Equals(n Node) bool

func (*Any) String

func (*Any) String() string

type Array

type Array struct {
	Type Node
}

func (*Array) Combine

func (a *Array) Combine(n Node) (Node, error)

func (*Array) Equals

func (a *Array) Equals(n Node) bool

func (*Array) String

func (a *Array) String() string

type CombinationError

type CombinationError struct {
	A, B Node
}

func (CombinationError) Error

func (e CombinationError) Error() string

type Node

type Node interface {
	fmt.Stringer

	Combine(Node) (Node, error)
	Equals(Node) bool
	// contains filtered or unexported methods
}

func Construct

func Construct(r io.Reader) (Node, error)
Example (Nullable)
n, _ := Construct(strings.NewReader(`[ { "nullable": null }, { "nullable": "null" } ]`))
fmt.Println(n)
Output:

[]struct{nullable *string}

type Number

type Number struct{}

func (*Number) Combine

func (i *Number) Combine(n Node) (Node, error)

func (*Number) Equals

func (i *Number) Equals(n Node) bool

func (*Number) String

func (*Number) String() string

type Optional

type Optional struct {
	Type Node
}

func (*Optional) Combine

func (o *Optional) Combine(n Node) (Node, error)

func (*Optional) Equals

func (o *Optional) Equals(n Node) bool

func (*Optional) String

func (o *Optional) String() string

type Or

type Or struct {
	Types []Node
}

func NewOr added in v0.1.1

func NewOr(types []Node) *Or

func (*Or) Combine

func (o *Or) Combine(n Node) (Node, error)

func (*Or) Equals

func (o *Or) Equals(n Node) bool

func (*Or) String

func (o *Or) String() string

type String

type String struct{}

func (*String) Combine

func (s *String) Combine(n Node) (Node, error)

func (*String) Equals

func (s *String) Equals(n Node) bool

func (*String) String

func (*String) String() string

type Struct

type Struct struct {
	SortedKeys []string
	Types      map[string]Node
}

func NewStruct

func NewStruct(types map[string]Node) *Struct

func (*Struct) Combine

func (s *Struct) Combine(n Node) (Node, error)

func (*Struct) Equals

func (s *Struct) Equals(n Node) bool

func (*Struct) String

func (s *Struct) String() string

Directories

Path Synopsis
fmt

Jump to

Keyboard shortcuts

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