From 5f9638076d3fa0e613e8d33372029015f52204e4 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Date: Sat, 16 Sep 2023 20:58:17 +0530 Subject: [PATCH] added serdes test --- .github/workflows/cicd.yaml | 21 ++++++++ .vscode/extensions.json | 8 +++ .vscode/settings.json | 27 +++++++++++ api.yaml | 97 +++++++++++++++++++------------------ go.mod | 4 +- go.sum | 4 ++ log.go | 7 +++ serdes.go | 23 +++++++++ serdes_test.go | 28 +++++++++++ spec.go | 31 +++++++++--- 10 files changed, 193 insertions(+), 57 deletions(-) create mode 100644 .github/workflows/cicd.yaml create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 go.sum create mode 100644 log.go create mode 100644 serdes.go create mode 100644 serdes_test.go diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml new file mode 100644 index 0000000..ec7a163 --- /dev/null +++ b/.github/workflows/cicd.yaml @@ -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 ./... diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..4b74574 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,8 @@ +{ + "recommendations": [ + "golang.go", + "streetsidesoftware.code-spell-checker", + "redhat.vscode-yaml", + "eamodio.gitlens", + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3621861 --- /dev/null +++ b/.vscode/settings.json @@ -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, +} \ No newline at end of file diff --git a/api.yaml b/api.yaml index 56076ab..8c983b2 100644 --- a/api.yaml +++ b/api.yaml @@ -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: | @@ -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: diff --git a/go.mod b/go.mod index 6e3786a..67792b1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a62c313 --- /dev/null +++ b/go.sum @@ -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= diff --git a/log.go b/log.go new file mode 100644 index 0000000..fcc1c62 --- /dev/null +++ b/log.go @@ -0,0 +1,7 @@ +package spec + +import ( + "log/slog" +) + +var log = slog.Default() diff --git a/serdes.go b/serdes.go new file mode 100644 index 0000000..f3f4aec --- /dev/null +++ b/serdes.go @@ -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 +} diff --git a/serdes_test.go b/serdes_test.go new file mode 100644 index 0000000..4f8cfe5 --- /dev/null +++ b/serdes_test.go @@ -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 + } + }) + } +} diff --git a/spec.go b/spec.go index c28e0f7..ece7ac7 100644 --- a/spec.go +++ b/spec.go @@ -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"` } @@ -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 @@ -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"`