Beispiel #1
0
func (g *validation) generateTests(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, idx int, patterns map[string]string) {

	if limbo.IsRequiredProperty(field) {
		g.generateRequiredTest(msg, field, fieldName)
	}

	if n, ok := limbo.GetMinItems(field); ok {
		g.generateMinItemsTest(msg, field, fieldName, int(n))
	}

	if n, ok := limbo.GetMaxItems(field); ok {
		g.generateMaxItemsTest(msg, field, fieldName, int(n))
	}

	if pattern, ok := limbo.GetPattern(field); ok {
		patternVar := fmt.Sprintf("valPattern_%s_%d", msg.GetName(), idx)
		patterns[patternVar] = pattern
		g.generatePatternTest(msg, field, fieldName, pattern, patternVar)
	}

	if n, ok := limbo.GetMinLength(field); ok {
		g.generateMinLengthTest(msg, field, fieldName, int(n))
	}

	if n, ok := limbo.GetMaxLength(field); ok {
		g.generateMaxLengthTest(msg, field, fieldName, int(n))
	}

	if field.GetType() == pb.FieldDescriptorProto_TYPE_MESSAGE {
		g.generateSubMessageTest(msg, field, fieldName)
	}

}
Beispiel #2
0
func (g *jsonschema) fieldToSchema(field *pb.FieldDescriptorProto) (map[string]interface{}, string) {
	if field.Options != nil {
		v, _ := proto.GetExtension(field.Options, limbo.E_HideInSwagger)
		hidePtr, _ := v.(*bool)
		if hidePtr != nil && *hidePtr == true {
			return nil, ""
		}
	}

	var (
		def map[string]interface{}
		dep string
	)

	switch field.GetType() {

	case pb.FieldDescriptorProto_TYPE_BOOL:
		def = map[string]interface{}{
			"type": "boolean",
		}

	case pb.FieldDescriptorProto_TYPE_FLOAT:
		def = map[string]interface{}{
			"type":   "number",
			"format": "float",
		}

	case pb.FieldDescriptorProto_TYPE_DOUBLE:
		def = map[string]interface{}{
			"type":   "number",
			"format": "double",
		}

	case pb.FieldDescriptorProto_TYPE_FIXED32,
		pb.FieldDescriptorProto_TYPE_FIXED64,
		pb.FieldDescriptorProto_TYPE_UINT32,
		pb.FieldDescriptorProto_TYPE_UINT64:
		def = map[string]interface{}{
			"type": "integer",
		}

	case pb.FieldDescriptorProto_TYPE_INT32,
		pb.FieldDescriptorProto_TYPE_SFIXED32,
		pb.FieldDescriptorProto_TYPE_SINT32:
		def = map[string]interface{}{
			"type":   "integer",
			"format": "int32",
		}

	case pb.FieldDescriptorProto_TYPE_INT64,
		pb.FieldDescriptorProto_TYPE_SFIXED64,
		pb.FieldDescriptorProto_TYPE_SINT64:
		def = map[string]interface{}{
			"type":   "integer",
			"format": "int64",
		}

	case pb.FieldDescriptorProto_TYPE_STRING:
		def = map[string]interface{}{
			"type": "string",
		}
		if x, ok := limbo.GetFormat(field); ok {
			def["format"] = x
		}
		if x, ok := limbo.GetPattern(field); ok {
			def["pattern"] = x
		}
		if x, ok := limbo.GetMinLength(field); ok {
			def["minLength"] = x
		}
		if x, ok := limbo.GetMaxLength(field); ok {
			def["maxLength"] = x
		}

	case pb.FieldDescriptorProto_TYPE_BYTES:
		def = map[string]interface{}{
			"type":   "string",
			"format": "base64",
		}

	case pb.FieldDescriptorProto_TYPE_ENUM:
		dep = strings.TrimPrefix(field.GetTypeName(), ".")
		def = map[string]interface{}{
			"$ref": dep,
		}

	case pb.FieldDescriptorProto_TYPE_MESSAGE:
		dep = strings.TrimPrefix(field.GetTypeName(), ".")
		def = map[string]interface{}{
			"$ref": dep,
		}

	default:
		panic("unsupported " + field.GetType().String())

	}

	if field.IsRepeated() {
		def = map[string]interface{}{
			"type":  "array",
			"items": def,
		}
		if x, ok := limbo.GetMinItems(field); ok {
			def["minItems"] = x
		}
		if x, ok := limbo.GetMaxItems(field); ok {
			def["maxItems"] = x
		}
	}

	return def, dep
}