• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang validation.NewSwaggerSchemaFromBytes函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中k8s/io/kubernetes/pkg/api/validation.NewSwaggerSchemaFromBytes函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSwaggerSchemaFromBytes函数的具体用法?Golang NewSwaggerSchemaFromBytes怎么用?Golang NewSwaggerSchemaFromBytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewSwaggerSchemaFromBytes函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: getSchemaAndValidate

func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) {
	var schemaData []byte
	fullDir, err := substituteUserHome(cacheDir)
	if err != nil {
		return err
	}
	cacheFile := path.Join(fullDir, prefix, groupVersion, schemaFileName)

	if len(cacheDir) != 0 {
		if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) {
			return err
		}
	}
	if schemaData == nil {
		schemaData, err = c.Get().
			AbsPath("/swaggerapi", prefix, groupVersion).
			Do().
			Raw()
		if err != nil {
			return err
		}
		if len(cacheDir) != 0 {
			if err := writeSchemaFile(schemaData, fullDir, cacheFile, prefix, groupVersion); err != nil {
				return err
			}
		}
	}
	schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
	if err != nil {
		return err
	}
	return schema.ValidateBytes(data)
}
开发者ID:kcbabo,项目名称:origin,代码行数:33,代码来源:factory.go


示例2: loadSchemaForTest

func loadSchemaForTest() (validation.Schema, error) {
	pathToSwaggerSpec := "../../../../api/swagger-spec/" + registered.GroupOrDie(api.GroupName).GroupVersion.Version + ".json"
	data, err := ioutil.ReadFile(pathToSwaggerSpec)
	if err != nil {
		return nil, err
	}
	return validation.NewSwaggerSchemaFromBytes(data, nil)
}
开发者ID:ravihansa3000,项目名称:kubernetes,代码行数:8,代码来源:factory_test.go


示例3: loadSchemaForTest

func loadSchemaForTest() (validation.Schema, error) {
	pathToSwaggerSpec := "../../../../api/swagger-spec/" + testapi.Default.GroupVersion().Version + ".json"
	data, err := ioutil.ReadFile(pathToSwaggerSpec)
	if err != nil {
		return nil, err
	}
	return validation.NewSwaggerSchemaFromBytes(data)
}
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:8,代码来源:factory_test.go


示例4: getSchemaAndValidate

func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) {
	var schemaData []byte
	var firstSeen bool
	fullDir, err := substituteUserHome(cacheDir)
	if err != nil {
		return err
	}
	cacheFile := path.Join(fullDir, prefix, groupVersion, schemaFileName)

	if len(cacheDir) != 0 {
		if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) {
			return err
		}
	}
	if schemaData == nil {
		firstSeen = true
		schemaData, err = downloadSchemaAndStore(c, cacheDir, fullDir, cacheFile, prefix, groupVersion)
		if err != nil {
			return err
		}
	}
	schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
	if err != nil {
		return err
	}
	err = schema.ValidateBytes(data)
	if _, ok := err.(validation.TypeNotFoundError); ok && !firstSeen {
		// As a temporay hack, kubectl would re-get the schema if validation
		// fails for type not found reason.
		// TODO: runtime-config settings needs to make into the file's name
		schemaData, err = downloadSchemaAndStore(c, cacheDir, fullDir, cacheFile, prefix, groupVersion)
		if err != nil {
			return err
		}
		schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
		if err != nil {
			return err
		}
		return schema.ValidateBytes(data)
	}

	return err
}
开发者ID:xiaohui,项目名称:kubernetes,代码行数:43,代码来源:factory.go


示例5: getSchemaAndValidate

func getSchemaAndValidate(c *client.RESTClient, data []byte, group, version string) error {
	schemaData, err := c.Get().
		AbsPath("/swaggerapi", group, version).
		Do().
		Raw()
	if err != nil {
		return err
	}
	schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
	if err != nil {
		return err
	}
	return schema.ValidateBytes(data)
}
开发者ID:rimey,项目名称:kubernetes,代码行数:14,代码来源:factory.go


示例6: ValidateBytes

func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
	version, _, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data)
	if err != nil {
		return err
	}
	if ok := registered.IsRegisteredAPIVersion(version); !ok {
		return fmt.Errorf("API version %q isn't supported, only supports API versions %q", version, registered.RegisteredVersions)
	}
	schemaData, err := c.c.RESTClient.Get().
		AbsPath("/swaggerapi/api", version).
		Do().
		Raw()
	if err != nil {
		return err
	}
	schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
	if err != nil {
		return err
	}
	return schema.ValidateBytes(data)
}
开发者ID:gogogocheng,项目名称:kubernetes,代码行数:21,代码来源:factory.go


示例7: getSchemaAndValidate

func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) {
	var schemaData []byte
	cacheFile := path.Join(cacheDir, prefix, groupVersion, schemaFileName)

	if len(cacheDir) != 0 {
		if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) {
			return err
		}
	}
	if schemaData == nil {
		schemaData, err = c.Get().
			AbsPath("/swaggerapi", prefix, groupVersion).
			Do().
			Raw()
		if err != nil {
			return err
		}
		if len(cacheDir) != 0 {
			if err = os.MkdirAll(path.Join(cacheDir, prefix, groupVersion), 0755); err != nil {
				return err
			}
			tmpFile, err := ioutil.TempFile(cacheDir, "schema")
			if err != nil {
				return err
			}
			if _, err := io.Copy(tmpFile, bytes.NewBuffer(schemaData)); err != nil {
				return err
			}
			if err := os.Link(tmpFile.Name(), cacheFile); err != nil && !os.IsExist(err) {
				return err
			}
		}
	}
	schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
	if err != nil {
		return err
	}
	return schema.ValidateBytes(data)
}
开发者ID:previousnext,项目名称:kube-ingress,代码行数:39,代码来源:factory.go


示例8: TestPerform

func TestPerform(t *testing.T) {
	tests := []struct {
		name        string
		namespace   string
		reader      io.Reader
		count       int
		swaggerFile string
		err         bool
		errMessage  string
	}{
		{
			name:      "Valid input",
			namespace: "test",
			reader:    strings.NewReader(guestbookManifest),
			count:     6,
		}, {
			name:       "Empty manifests",
			namespace:  "test",
			reader:     strings.NewReader(""),
			err:        true,
			errMessage: "no objects visited",
		}, {
			name:        "Invalid schema",
			namespace:   "test",
			reader:      strings.NewReader(testInvalidServiceManifest),
			swaggerFile: "../../vendor/k8s.io/kubernetes/api/swagger-spec/" + testapi.Default.GroupVersion().Version + ".json",
			err:         true,
			errMessage:  `error validating "": error validating data: expected type int, for field spec.ports[0].port, got string`,
		},
	}

	for _, tt := range tests {
		results := []*resource.Info{}

		fn := func(info *resource.Info) error {
			results = append(results, info)

			if info.Namespace != tt.namespace {
				t.Errorf("%q. expected namespace to be '%s', got %s", tt.name, tt.namespace, info.Namespace)
			}
			return nil
		}

		f, tf, _, _ := cmdtesting.NewAPIFactory()
		c := &Client{Factory: f}
		if tt.swaggerFile != "" {
			data, err := ioutil.ReadFile(tt.swaggerFile)
			if err != nil {
				t.Fatalf("could not read swagger spec: %s", err)
			}
			validator, err := validation.NewSwaggerSchemaFromBytes(data, nil)
			if err != nil {
				t.Fatalf("could not load swagger spec: %s", err)
			}
			tf.Validator = validator
		}

		err := perform(c, tt.namespace, tt.reader, fn)
		if (err != nil) != tt.err {
			t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
		}
		if err != nil && err.Error() != tt.errMessage {
			t.Errorf("%q. expected error message: %v, got %v", tt.name, tt.errMessage, err)
		}

		if len(results) != tt.count {
			t.Errorf("%q. expected %d result objects, got %d", tt.name, tt.count, len(results))
		}
	}
}
开发者ID:kubernetes,项目名称:helm,代码行数:70,代码来源:client_test.go



注:本文中的k8s/io/kubernetes/pkg/api/validation.NewSwaggerSchemaFromBytes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang validation.ValidateAnnotations函数代码示例发布时间:2022-05-28
下一篇:
Golang validation.NameIsDNSSubdomain函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap