Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Rename version to api_version
Browse files Browse the repository at this point in the history
refs #1199
  • Loading branch information
carmenlau committed Jan 14, 2020
2 parents 171b261 + c776d48 commit e2b2d79
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 77 deletions.
53 changes: 53 additions & 0 deletions pkg/core/apiversion/apiversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package apiversion

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
)

// MajorVersion is the current major API Version.
const MajorVersion = 2

// MinorVersion is the current minor API Version.
const MinorVersion = 1

// APIVersion is the current API Version.
var APIVersion = Format(MajorVersion, MinorVersion)

// SupportedVersions is a slice of supported versions.
var SupportedVersions []string

// SupportedVersionsJSON is an JSON array of supported versions.
var SupportedVersionsJSON string

var regexpAPIVersion = regexp.MustCompile(`^v(\d+)\.(\d+)$`)

func init() {
for i := 0; i <= MinorVersion; i++ {
SupportedVersions = append(SupportedVersions, Format(MajorVersion, i))
}
bytes, err := json.Marshal(SupportedVersions)
if err != nil {
panic(err)
}
SupportedVersionsJSON = string(bytes)
}

// Format formats major and minor into `v<major>.<minor>`.
func Format(major, minor int) string {
return fmt.Sprintf("v%d.%d", major, minor)
}

// Parse parses API version into major and minor.
func Parse(apiVersion string) (major int, minor int, ok bool) {
output := regexpAPIVersion.FindAllStringSubmatch(apiVersion, -1)
if len(output) <= 0 {
return
}
major, _ = strconv.Atoi(output[0][1])
minor, _ = strconv.Atoi(output[0][2])
ok = true
return
}
28 changes: 28 additions & 0 deletions pkg/core/apiversion/apiversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package apiversion

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestFormat(t *testing.T) {
Convey("Format", t, func() {
So(Format(1, 2), ShouldEqual, "v1.2")
})
}

func TestParse(t *testing.T) {
Convey("Parse", t, func() {
var major, minor int
var ok bool

major, minor, ok = Parse("v1.2")
So(ok, ShouldBeTrue)
So(major, ShouldEqual, 1)
So(minor, ShouldEqual, 2)

_, _, ok = Parse("v1.2 ")
So(ok, ShouldBeFalse)
})
}
16 changes: 9 additions & 7 deletions pkg/core/config/schema.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package config

import (
"fmt"
"io"

"github.com/skygeario/skygear-server/pkg/core/apiversion"
"github.com/skygeario/skygear-server/pkg/core/validation"
)

const (
var (
// Schemas is public so that the schemas can be composed in other contexts.
Schemas = `
Schemas = fmt.Sprintf(`
{
"NonEmptyString": {
"$id": "#NonEmptyString",
Expand All @@ -24,14 +26,14 @@ const (
"$id": "#TenantConfiguration",
"type": "object",
"properties": {
"version": { "const": "2" },
"api_version": { "enum": %s },
"app_id": { "$ref": "#NonEmptyString" },
"app_name": { "$ref": "#NonEmptyString" },
"database_config": { "$ref": "#DatabaseConfiguration" },
"hook": { "$ref": "#HookTenantConfiguration" },
"app_config": { "$ref": "#AppConfiguration" }
},
"required": ["version", "app_id", "app_name", "database_config", "app_config"]
"required": ["api_version", "app_id", "app_name", "database_config", "app_config"]
},
"DatabaseConfiguration": {
"$id": "#DatabaseConfiguration",
Expand All @@ -54,7 +56,7 @@ const (
"type": "object",
"additionalProperties": false,
"properties": {
"version": { "const": "2" },
"api_version": { "enum": %s },
"display_app_name": { "type": "string" },
"clients": {
"type": "array",
Expand All @@ -76,7 +78,7 @@ const (
"nexmo" : { "$ref": "#NexmoConfiguration" },
"asset": { "$ref": "#AssetConfiguration" }
},
"required": ["version", "master_key", "auth", "hook", "asset"]
"required": ["api_version", "master_key", "auth", "hook", "asset"]
},
"AssetConfiguration": {
"$id": "#AssetConfiguration",
Expand Down Expand Up @@ -536,7 +538,7 @@ const (
}
}
}
`
`, apiversion.SupportedVersionsJSON, apiversion.SupportedVersionsJSON)
)

var (
Expand Down
28 changes: 14 additions & 14 deletions pkg/core/config/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func TestParseAppConfiguration(t *testing.T) {
// Empty root
test(
`{}`,
"/api_version: Required",
"/asset: Required",
"/auth: Required",
"/hook: Required",
"/master_key: Required",
"/version: Required",
)
// Empty auth
test(`
Expand All @@ -36,10 +36,10 @@ func TestParseAppConfiguration(t *testing.T) {
"auth": {},
"hook": {}
}`,
"/api_version: Required",
"/asset: Required",
"/auth/authentication_session: Required",
"/hook/secret: Required",
"/version: Required",
)
// Empty auth.login_id_keys
test(`
Expand All @@ -51,11 +51,11 @@ func TestParseAppConfiguration(t *testing.T) {
},
"hook": {}
}`,
"/api_version: Required",
"/asset/secret: Required",
"/auth/authentication_session: Required",
"/auth/login_id_keys: EntryAmount map[gte:1]",
"/hook/secret: Required",
"/version: Required",
)
// Invalid login id type
test(`
Expand Down Expand Up @@ -97,17 +97,17 @@ func TestParseAppConfiguration(t *testing.T) {
},
"hook": {}
}`,
"/api_version: Required",
"/asset: Required",
"/auth/authentication_session: Required",
"/auth/login_id_keys/3/type: Enum map[expected:[raw email phone username]]",
"/auth/login_id_types/phone: ExtraEntry",
"/hook/secret: Required",
"/version: Required",
)
// Minimal valid example
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestParseAppConfiguration(t *testing.T) {
// API Clients
test(`
{
"version": "2",
"api_version": "v2.1",
"clients": [
{
"key": "web-app"
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestParseAppConfiguration(t *testing.T) {
// MFA
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestParseAppConfiguration(t *testing.T) {
// User Audit
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestParseAppConfiguration(t *testing.T) {
// WelcomeEmailConfiguration
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestParseAppConfiguration(t *testing.T) {
// CustomTokenConfiguration
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestParseAppConfiguration(t *testing.T) {
// OAuth
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestParseAppConfiguration(t *testing.T) {
// UserVerificationConfiguration
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestParseAppConfiguration(t *testing.T) {
// SMTP config
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestParseAppConfiguration(t *testing.T) {
// Nexmo config
test(`
{
"version": "2",
"api_version": "v2.1",
"master_key": "master_key",
"asset": {
"secret": "assetsecret"
Expand Down
10 changes: 2 additions & 8 deletions pkg/core/config/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ import (
"github.com/skygeario/skygear-server/pkg/core/validation"
)

// TenantConfigurationVersion is the latest version of TenantConfiguration.
const TenantConfigurationVersion = "2"

// AppConfigurationVersion is the latest version of AppConfiguration.
const AppConfigurationVersion = "2"

//go:generate msgp -tests=false
type TenantConfiguration struct {
Version string `json:"version,omitempty" yaml:"version" msg:"version"`
APIVersion string `json:"api_version,omitempty" yaml:"api_version" msg:"api_version"`
AppID string `json:"app_id,omitempty" yaml:"app_id" msg:"app_id"`
AppName string `json:"app_name,omitempty" yaml:"app_name" msg:"app_name"`
Hook *HookTenantConfiguration `json:"hook,omitempty" yaml:"hook" msg:"hook" default_zero_value:"true"`
Expand Down Expand Up @@ -517,7 +511,7 @@ func WriteTenantConfig(r *http.Request, config *TenantConfiguration) {

// AppConfiguration represents user-editable configuration
type AppConfiguration struct {
Version string `json:"version,omitempty" yaml:"version" msg:"version"`
APIVersion string `json:"api_version,omitempty" yaml:"api_version" msg:"api_version"`
DisplayAppName string `json:"display_app_name,omitempty" yaml:"display_app_name" msg:"display_app_name"`
Clients []APIClientConfiguration `json:"clients,omitempty" yaml:"clients" msg:"clients"`
MasterKey string `json:"master_key,omitempty" yaml:"master_key" msg:"master_key"`
Expand Down
Loading

0 comments on commit e2b2d79

Please sign in to comment.