Skip to content

Commit

Permalink
added serdes test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutshkumr committed Sep 16, 2023
1 parent 1305134 commit 5f96380
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 57 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI/CD

on:
push:
pull_request:

jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
submodules: "true"

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.21"

- name: Unit Test
run: go test -v -count 1 -timeout 30s ./...
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"golang.go",
"streetsidesoftware.code-spell-checker",
"redhat.vscode-yaml",
"eamodio.gitlens",
]
}
27 changes: 27 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"editor.rulers": [
80,
90
],
"go.useLanguageServer": true,
"go.testEnvVars": {
"CGO_ENABLED": 0,
},
"go.testFlags": [
"-v",
"-count",
"1"
],
"editor.formatOnSave": true,
"gopls": {
"build.env": {
"CGO_ENABLED": 0,
}
},
"go.toolsEnvVars": {
"CGO_ENABLED": 0
},
"files.insertFinalNewline": true,
"files.eol": "\n",
"diffEditor.ignoreTrimWhitespace": false,
}
97 changes: 49 additions & 48 deletions api.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
openapi: 3.1.0
# openapi: 3.1.0

info:
x-api-version: 0.0.1
title: xapi
version: 0.0.1
description: |
Expand All @@ -9,56 +10,56 @@ info:
name: MIT
url: https://github.com/xapi-tools/xapic/blob/main/license

servers:
- url: localhost:8443
# servers:
# - url: localhost:8443

security:
- {}
# security:
# - {}

paths:
/config:
get:
operationId: get_config
summary: Get configuration from server
tags:
- config
responses:
200:
description: Successfully returned configuration
content:
application/json:
schema:
$ref: "#/components/schemas/Config"
default:
description: Failed to return configuration
content:
application/json:
schema:
$ref: "#/components/schemas/ResponseError"
post:
operationId: push_config
summary: Push configuration to server
tags:
- config
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Config"
# paths:
# /config:
# get:
# operationId: get_config
# summary: Get configuration from server
# tags:
# - config
# responses:
# 200:
# description: Successfully returned configuration
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/Config"
# default:
# description: Failed to return configuration
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/ResponseError"
# post:
# operationId: push_config
# summary: Push configuration to server
# tags:
# - config
# requestBody:
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/Config"

responses:
200:
description: Successfully pushed configuration
content:
application/json:
schema:
$ref: "#/components/schemas/ResponseOk"
default:
description: Failed pushing configuration
content:
application/json:
schema:
$ref: "#/components/schemas/ResponseError"
# responses:
# 200:
# description: Successfully pushed configuration
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/ResponseOk"
# default:
# description: Failed pushing configuration
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/ResponseError"

components:
schemas:
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/xapi-tools/api
module github.com/xapi-tools/spec

go 1.21.1

require gopkg.in/yaml.v3 v3.0.1
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 7 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spec

import (
"log/slog"
)

var log = slog.Default()
23 changes: 23 additions & 0 deletions serdes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spec

import (
"fmt"
"os"

"gopkg.in/yaml.v3"
)

func NewSpecFromFile(path string) (*Spec, error) {
log.Info("Parsing spec from file", "path", path)
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read file %s: %v", path, err)
}

s := &Spec{}
if err := yaml.Unmarshal(b, s); err != nil {
return nil, fmt.Errorf("could not unmarshal file contents: %v", err)
}

return s, nil
}
28 changes: 28 additions & 0 deletions serdes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package spec

import (
"testing"
)

func TestNewSpecFromFile(t *testing.T) {

tests := []struct {
name string
path string
wantErr bool
}{
{
name: "basic",
path: "api.yaml",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewSpecFromFile(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("NewSpecFromFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
31 changes: 23 additions & 8 deletions spec.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package api
package spec

type Spec struct {
Info Info `yaml:"info,omitempty"`
Components Components `yaml:"components,omitempty"`
}

type Info struct {
Title string `yaml:"title,omitempty"`
Version string `yaml:"version,omitempty"`
Description string `yaml:"description,omitempty"`
License License `yaml:"license,omitempty"`

XApiVersion string `yaml:"x-api-version,omitempty"`
}

type License struct {
Name string `yaml:"name,omitempty"`
Url string `yaml:"url,omitempty"`
}

type Components struct {
Schemas Schemas `yaml:"schemas,omitempty"`
}
Expand All @@ -27,7 +42,7 @@ type Schema struct {
// When set to true
// - exactly one property in the schema MUST be set at all times
// - list of required properties in the schema MUST be empty
XApiUnion bool `yaml:"x-api-union,omitempty"`
XApiChoice bool `yaml:"x-api-choice,omitempty"`
}

type Properties map[string]Property
Expand All @@ -38,12 +53,12 @@ type Property struct {
Ref *string `yaml:"$ref,omitempty"`

// Possible values are `string`, `integer`, `number`, `boolean` and `array`
Type string `yaml:"type,omitempty"`
Format *string `yaml:"format,omitempty"`
Pattern *string `yaml:"pattern,omitempty"`
Min *string `yaml:"min,omitempty"`
Max *string `yaml:"max,omitempty"`
Default *string `yaml:"default,omitempty"`
Type string `yaml:"type,omitempty"`
Format *string `yaml:"format,omitempty"`
Pattern *string `yaml:"pattern,omitempty"`
Min *string `yaml:"min,omitempty"`
Max *string `yaml:"max,omitempty"`
Default interface{} `yaml:"default,omitempty"`

Enum []string `yaml:"enum,omitempty"`

Expand Down

0 comments on commit 5f96380

Please sign in to comment.