Skip to content

Commit

Permalink
swagger command
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed May 25, 2024
1 parent 2c6b4de commit 9f6f9bc
Show file tree
Hide file tree
Showing 19 changed files with 1,081 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Set `cmd = "go build -o ./tmp/main.exe ./examples/MY_EXAMPLE"`.
Set `include_ext = ["go", "tpl", "tmpl", "html", "js"]`.

```bash
air serve --port 8080
air edit --port 8080
```

As you hit save on your go code, the webserver will restart, and the web page will automatically refresh itself.
Expand All @@ -92,10 +92,10 @@ If you want to mess with modern web browser features and need https, I recommend
```bash
mkcert -install
mkcert -key-file key.pem -cert-file cert.pem localhost
air serve --port 8080 --ssl
air edit --port 8080 --ssl

# And if you want to connect with your headset
air serve --port 8080 --ssl --host 0.0.0.0
air edit --port 8080 --ssl --host 0.0.0.0
```

## WASM Dev
Expand Down
45 changes: 33 additions & 12 deletions examples/edit-gaussian-splats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"bufio"
"bytes"
"errors"
"io"
"math"
"os"

Expand All @@ -20,20 +23,34 @@ import (
"github.com/EliCDavis/vector/vector4"
)

type PointcloudLoaderNode = nodes.StructNode[modeling.Mesh, PointcloudLoaderNodeData]
type FileNode = nodes.StructNode[[]byte, FileNodeData]

type PointcloudLoaderNodeData struct {
type FileNodeData struct {
Path nodes.NodeOutput[string]
}

func (pn PointcloudLoaderNodeData) Process() (modeling.Mesh, error) {
f, err := os.Open(pn.Path.Value())
func (fnd FileNodeData) Process() ([]byte, error) {
if fnd.Path == nil {
return nil, errors.New("no path specified")
}

f, err := os.Open(fnd.Path.Value())
if err != nil {
return modeling.EmptyMesh(modeling.PointTopology), err
return nil, err
}
defer f.Close()

bufReader := bufio.NewReader(f)
return io.ReadAll(f)
}

type PointcloudLoaderNode = nodes.StructNode[modeling.Mesh, PointcloudLoaderNodeData]

type PointcloudLoaderNodeData struct {
Data nodes.NodeOutput[[]byte]
}

func (pn PointcloudLoaderNodeData) Process() (modeling.Mesh, error) {
bufReader := bufio.NewReader(bytes.NewReader(pn.Data.Value()))

header, err := ply.ReadHeader(bufReader)
if err != nil {
Expand Down Expand Up @@ -175,12 +192,16 @@ func main() {

pointcloud := &PointcloudLoaderNode{
Data: PointcloudLoaderNodeData{
Path: &generator.ParameterNode[string]{
Name: "Pointcloud Path",
DefaultValue: "./point_cloud/iteration_30000/point_cloud.ply",
CLI: &generator.CliParameterNodeConfig[string]{
FlagName: "splat",
Usage: "Path to the guassian splat to load (PLY file)",
Data: &FileNode{
Data: FileNodeData{
Path: &generator.ParameterNode[string]{
Name: "Pointcloud Path",
DefaultValue: "./point_cloud/iteration_30000/point_cloud.ply",
CLI: &generator.CliParameterNodeConfig[string]{
FlagName: "splat",
Usage: "Path to the guassian splat to load (PLY file)",
},
},
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions formats/swagger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Swagger 2.0

Loose implementation of [Swagger 2.0 specification](https://swagger.io/specification/v2/) for the needs of specifically polyform. This does not aim to be a full fledged implementation, so be cautious before considering using anything here for personal purposes.
51 changes: 51 additions & 0 deletions formats/swagger/definitions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package swagger

const Vector2DefinitionName = "Vector2"
const Vector3DefinitionName = "Vector3"
const Vector4DefinitionName = "Vector4"
const AABBDefinitionName = "AABB"

var vectorComponent = Property{
Type: NumberPropertyType,
Format: DoublePropertyFormat,
Example: "1.0",
}

var Vector2Definition = Definition{
Type: "object",
Properties: map[string]Property{
"x": vectorComponent,
"y": vectorComponent,
},
}

var Vector3Definition = Definition{
Type: "object",
Properties: map[string]Property{
"x": vectorComponent,
"y": vectorComponent,
"z": vectorComponent,
},
}

var Vector4Definition = Definition{
Type: "object",
Properties: map[string]Property{
"x": vectorComponent,
"y": vectorComponent,
"z": vectorComponent,
"w": vectorComponent,
},
}

var AABBDefinition = Definition{
Type: "object",
Properties: map[string]Property{
"min": {
Ref: DefinitionRefPath(Vector3DefinitionName),
},
"max": {
Ref: DefinitionRefPath(Vector3DefinitionName),
},
},
}
47 changes: 47 additions & 0 deletions formats/swagger/parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package swagger

type ParameterLocation string

const (
PathParameterLocation ParameterLocation = "path"
QueryParameterLocation ParameterLocation = "query"
HeaderParameterLocation ParameterLocation = "header"
BodyParameterLocation ParameterLocation = "body"
FormParameterLocation ParameterLocation = "formData"
)

type Parameter struct {
// Required. The location of the parameter. Possible values are "query",
// "header", "path", "formData" or "body".
In ParameterLocation `json:"in"`

// Required. The name of the parameter. Parameter names are case sensitive.
// * If in is "path", the name field MUST correspond to the associated
// path segment from the path field in the Paths Object. See Path
// Templating for further information.
// * For all other cases, the name corresponds to the parameter name used
// based on the in property
Name string `json:"name,omitempty"`

// A brief description of the parameter. This could contain examples of
// use. GFM syntax can be used for rich text representation.
Description string `json:"description,omitempty"`

// Determines whether this parameter is mandatory. If the parameter is in
// "path", this property is required and its value MUST be true. Otherwise,
// the property MAY be included and its default value is false.
Required bool `json:"required,omitempty"`

// If in is "body": Required. The schema defining the type used for the
// body parameter.
Schema any `json:"schema,omitempty"`

// If in is any value other than "body"
// Required. The type of the parameter. Since the parameter is not located
// at the request body, it is limited to simple types (that is, not an
// object). The value MUST be one of "string", "number", "integer",
// "boolean", "array" or "file". If type is "file", the consumes MUST be
// either "multipart/form-data", " application/x-www-form-urlencoded" or
// both and the parameter MUST be in "formData".
Type string `json:"type,omitempty"`
}
58 changes: 58 additions & 0 deletions formats/swagger/property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package swagger

type PropertyType string

const (
ObjectPropertyType PropertyType = "object"
StringPropertyType PropertyType = "string"
IntegerPropertyType PropertyType = "integer"
NumberPropertyType PropertyType = "number"
BooleanPropertyType PropertyType = "boolean"
ArrayPropertyType PropertyType = "array"
)

type PropertyFormat string

const (
// Integer ================================================================

Int32PropertyFormat PropertyFormat = "int32"
Int64PropertyFormat PropertyFormat = "int64"

// Number =================================================================

FloatPropertyFormat PropertyFormat = "float"
DoublePropertyFormat PropertyFormat = "double"

// String =================================================================

// Base64-encoded characters, for example, U3dhZ2dlciByb2Nrcw==
BytePropertyFormat PropertyFormat = "byte"

// Binary data, used to describe files
BinaryPropertyFormat PropertyFormat = "binary"

// Full-date notation as defined by RFC 3339, section 5.6, for example,
// 2017-07-21
DatePropertyFormat PropertyFormat = "date"

// The date-time notation as defined by RFC 3339, section 5.6, for example,
// 2017-07-21T17:32:28Z
DateTimePropertyFormat PropertyFormat = "date-time"

// A hint to UIs to mask the input
PasswordPropertyFormat PropertyFormat = "password"
)

type Property struct {
Type PropertyType `json:"type,omitempty"`

// An optional format modifier serves as a hint at the contents and format
// of the string
Format PropertyFormat `json:"format,omitempty"`

Example any `json:"example,omitempty"`
Ref any `json:"$ref,omitempty"`
Description any `json:"description,omitempty"`
Items any `json:"items,omitempty"`
}
61 changes: 61 additions & 0 deletions formats/swagger/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package swagger

import "errors"

func DefinitionRefPath(r string) string {
if r == "" {
panic(errors.New("can not build definition reference from an empty string"))
}
return "#/definitions/" + r
}

type SchemaObject struct {
Ref string `json:"$ref,omitempty"`
}

type RequestMethod string

const (
GetRequestMethod RequestMethod = "get"
PostRequestMethod RequestMethod = "post"
)

type Info struct {
Title string `json:"title"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
}

type Response struct {
Description string `json:"description,omitempty"`
// This is a lookup into the responses section, not definitions
Ref string `json:"$ref,omitempty"`
Schema any `json:"schema,omitempty"`
}

type RequestDefinition struct {
Summary string `json:"summary"`
Description string `json:"description"`
Produces []string `json:"produces"`
Consumes []string `json:"consumes"`
Responses map[int]Response `json:"responses"`
Parameters []Parameter `json:"parameters"`
}

type Path map[RequestMethod]RequestDefinition

type Definition struct {
// Type is probably always "object"
Type string `json:"type"`
Properties map[string]Property `json:"properties"`
Required []string `json:"required,omitempty"`
Example any `json:"example,omitempty"`
}

type Spec struct {
Version string `json:"swagger"` // Must be 2.0
Info *Info `json:"info,omitempty"`
Paths map[string]Path `json:"paths"`
Definitions map[string]Definition `json:"definitions,omitempty"`
Responses map[string]Response `json:"responses,omitempty"`
}
32 changes: 32 additions & 0 deletions formats/swagger/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package swagger_test

import (
"testing"

"github.com/EliCDavis/polyform/formats/swagger"
"github.com/stretchr/testify/assert"
)

func TestDefinitionRef(t *testing.T) {
tests := map[string]struct {
input string
want string
}{
"simple": {
input: "simple",
want: "#/definitions/simple",
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, swagger.DefinitionRefPath(tc.input))
})
}
}

func TestDefinitionRef_PanicOnEmpty(t *testing.T) {
assert.PanicsWithError(t, "can not build definition reference from an empty string", func() {
swagger.DefinitionRefPath("")
})
}
Loading

0 comments on commit 9f6f9bc

Please sign in to comment.