MSP-8172 Add vendor
This commit is contained in:
1 parent
5175d87e08
commit
eb066cb055
2619 files changed
+757077
-2
No files matched your search
+39
@@ -0,0 +1,39 @@
|
||||
# Package `rct`
|
||||
|
||||
Пакет `rct` предназначен для тестирования схемы и валидации удалённой конфигурации, используемой с `isp-kit`.
|
||||
|
||||
## Functions
|
||||
|
||||
### `Test[T any](t *testing.T, defaultRemoteConfigPath string, remoteConfig T)`
|
||||
|
||||
Проверяет, что удалённая конфигурация соответствует сгенерированной JSON-схеме и проходит валидацию.
|
||||
|
||||
* Проверяет отсутствие устаревшего тега `valid`
|
||||
* Проверяет соответствие структуры JSON-схеме
|
||||
* Выполняет валидацию значений с помощью `validator.Default`
|
||||
|
||||
### `FindTag[T any](v T, tag string) bool`
|
||||
|
||||
Рекурсивно проверяет, содержит ли структура тег с указанным именем.
|
||||
|
||||
## Usage
|
||||
|
||||
### Example usage in test
|
||||
|
||||
```go
|
||||
package mypkg_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/txix-open/isp-kit/rct"
|
||||
)
|
||||
|
||||
type RemoteCfg struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
}
|
||||
|
||||
func TestRemoteCfg(t *testing.T) {
|
||||
rct.Test(t, "conf/default_remote_config.json", RemoteCfg{})
|
||||
}
|
||||
```
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"string": "value",
|
||||
"integer": 122,
|
||||
"child": {
|
||||
"value": "123"
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
// Package rct provides test helpers for remote configuration validation.
|
||||
// It validates that configuration structures match their JSON schemas and
|
||||
// can be properly unmarshaled from configuration files.
|
||||
package rct
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"github.com/txix-open/isp-kit/rc"
|
||||
"github.com/txix-open/isp-kit/validator"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
// Test validates that the provided configuration structure matches its
|
||||
// generated JSON schema and can be properly unmarshaled from the default
|
||||
// configuration file. It also verifies that the configuration passes
|
||||
// validator tag validation.
|
||||
//
|
||||
// The function checks:
|
||||
// - No deprecated 'valid' tags are present
|
||||
// - The default configuration file is valid against the generated schema
|
||||
// - The configuration can be unmarshaled from the file
|
||||
// - The configuration passes validation
|
||||
//
|
||||
// nolint:lll
|
||||
func Test[T any](t *testing.T, defaultRemoteConfigPath string, remoteConfig T) {
|
||||
require := require.New(t)
|
||||
|
||||
validatorTag := "valid"
|
||||
require.Falsef(FindTag(remoteConfig, validatorTag), "'%s' tag is not longer supported, you must use https://github.com/go-playground/validator", validatorTag)
|
||||
|
||||
defaultRemoteConfig, err := os.ReadFile(defaultRemoteConfigPath)
|
||||
require.NoError(err)
|
||||
|
||||
jsonSchema := rc.GenerateConfigSchema(remoteConfig)
|
||||
jsonSchemaData, err := json.Marshal(jsonSchema)
|
||||
require.NoError(err)
|
||||
|
||||
remoteConfigAsMap := make(map[string]any)
|
||||
err = json.Unmarshal(defaultRemoteConfig, &remoteConfigAsMap)
|
||||
require.NoError(err)
|
||||
|
||||
schemaLoader := gojsonschema.NewBytesLoader(jsonSchemaData)
|
||||
configLoader := gojsonschema.NewGoLoader(remoteConfigAsMap)
|
||||
result, err := gojsonschema.Validate(schemaLoader, configLoader)
|
||||
require.NoError(err)
|
||||
|
||||
for _, resultError := range result.Errors() {
|
||||
require.Empty(resultError.String())
|
||||
}
|
||||
|
||||
err = json.Unmarshal(defaultRemoteConfig, &remoteConfig)
|
||||
require.NoError(err)
|
||||
err = validator.Default.ValidateToError(remoteConfig)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
// FindTag recursively searches a type for the specified struct tag.
|
||||
// It traverses structs, maps, arrays, and slices to find any field
|
||||
// that has the given tag. Returns true if the tag is found.
|
||||
func FindTag[T any](v T, tag string) bool {
|
||||
t := reflect.TypeOf(v)
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
queue := []reflect.Type{t}
|
||||
for len(queue) > 0 {
|
||||
cur := queue[0]
|
||||
queue = queue[1:]
|
||||
if cur.Kind() == reflect.Ptr {
|
||||
cur = cur.Elem()
|
||||
}
|
||||
// nolint:exhaustive
|
||||
switch cur.Kind() {
|
||||
case reflect.Struct:
|
||||
for field := range cur.Fields() {
|
||||
if field.Tag.Get(tag) != "" {
|
||||
return true
|
||||
}
|
||||
queue = append(queue, field.Type)
|
||||
}
|
||||
case reflect.Map, reflect.Array, reflect.Slice:
|
||||
queue = append(queue, cur.Elem())
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user