func (g *validation) generateRequiredTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string) { if field.IsRepeated() { g.P(`if `, fieldName, ` == nil || len(`, fieldName, `) == 0 {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`) g.P(`}`) return } switch field.GetType() { case pb.FieldDescriptorProto_TYPE_BYTES: g.P(`if `, fieldName, ` == nil || len(`, fieldName, `) == 0 {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`) g.P(`}`) case pb.FieldDescriptorProto_TYPE_STRING: g.P(`if `, fieldName, ` == "" {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`) g.P(`}`) case pb.FieldDescriptorProto_TYPE_MESSAGE: if gogoproto.IsNullable(field) { g.P(`if `, fieldName, ` == nil {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`) g.P(`}`) } } }
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) } }
func (g *validation) generatePatternTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, pattern, patternVar string) { if field.GetType() != pb.FieldDescriptorProto_TYPE_STRING { g.gen.Fail("limbo.pattern can only be used on string fields.") } if field.IsRepeated() { g.P(`for idx, value :=range `, fieldName, `{`) g.P(`if !`, patternVar, `.MatchString(value) {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, `[%d] (%q) does not match pattern %s", idx, value, `, strconv.Quote(pattern), `)`) g.P(`}`) g.P(`}`) return } g.P(`if !`, patternVar, `.MatchString(`, fieldName, `) {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` (%q) does not match pattern %s", `, fieldName, `, `, strconv.Quote(pattern), `)`) g.P(`}`) }
func (g *validation) generateMaxLengthTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, maxLength int) { if field.GetType() != pb.FieldDescriptorProto_TYPE_STRING { g.gen.Fail("limbo.maxLength can only be used on string fields.") } if field.IsRepeated() { g.P(`for idx, value :=range `, fieldName, `{`) g.P(`if len(value) > `, maxLength, ` {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, `[%d] (%q) is to long (maximum length: `, maxLength, `)", idx, value)`) g.P(`}`) g.P(`}`) return } g.P(`if len(`, fieldName, `) > `, maxLength, ` {`) g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` (%q) is to long (maximum length: `, maxLength, `)", `, fieldName, `)`) g.P(`}`) }
func (p *plugin) GenerateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { proto3 := gogoproto.IsProto3(file.FileDescriptorProto) goTyp, _ := p.GoType(message, field) fieldname := p.GetOneOfFieldName(message, field) goTypName := generator.GoTypeToName(goTyp) if p.IsMap(field) { m := p.GoMapType(nil, field) keygoTyp, _ := p.GoType(nil, m.KeyField) keygoTyp = strings.Replace(keygoTyp, "*", "", 1) keygoAliasTyp, _ := p.GoType(nil, m.KeyAliasField) keygoAliasTyp = strings.Replace(keygoAliasTyp, "*", "", 1) valuegoTyp, _ := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) keytypName := generator.GoTypeToName(keygoTyp) keygoAliasTyp = generator.GoTypeToName(keygoAliasTyp) valuetypAliasName := generator.GoTypeToName(valuegoAliasTyp) nullable, valuegoTyp, valuegoAliasTyp := generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, m.GoType, `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() keyval := "" if m.KeyField.IsString() { keyval = fmt.Sprintf("randString%v(r)", p.localName) } else { keyval = value(keytypName, m.KeyField.GetType()) } if keygoAliasTyp != keygoTyp { keyval = keygoAliasTyp + `(` + keyval + `)` } if m.ValueField.IsMessage() || p.IsGroup(field) { s := `this.` + fieldname + `[` + keyval + `] = ` goTypName = generator.GoTypeToName(valuegoTyp) funcCall := getFuncCall(goTypName) if !nullable { funcCall = `*` + funcCall } if valuegoTyp != valuegoAliasTyp { funcCall = `(` + valuegoAliasTyp + `)(` + funcCall + `)` } s += funcCall p.P(s) } else if m.ValueField.IsEnum() { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + p.getEnumVal(m.ValueField, valuegoTyp) p.P(s) } else if m.ValueField.IsBytes() { count := p.varGen.Next() p.P(count, ` := r.Intn(100)`) p.P(p.varGen.Next(), ` := `, keyval) p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = make(`, valuegoTyp, `, `, count, `)`) p.P(`for i := 0; i < `, count, `; i++ {`) p.In() p.P(`this.`, fieldname, `[`, p.varGen.Current(), `][i] = byte(r.Intn(256))`) p.Out() p.P(`}`) } else if m.ValueField.IsString() { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + fmt.Sprintf("randString%v(r)", p.localName) p.P(s) } else { p.P(p.varGen.Next(), ` := `, keyval) p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = `, value(valuetypAliasName, m.ValueField.GetType())) if negative(m.ValueField.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] *= -1`) p.Out() p.P(`}`) } } p.Out() p.P(`}`) } else if field.IsMessage() || p.IsGroup(field) { funcCall := getFuncCall(goTypName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(5)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, `[i] = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, `[i] = *`, p.varGen.Current()) } p.Out() p.P(`}`) } else { if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, ` = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, ` = *`, p.varGen.Current()) } } } else { if field.IsEnum() { val := p.getEnumVal(field, goTyp) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, val) p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, val) } else { p.P(p.varGen.Next(), ` := `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } else if gogoproto.IsCustomType(field) { funcCall := getCustomFuncCall(goTypName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, `[i] = *`, p.varGen.Current()) p.Out() p.P(`}`) } else if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, ` = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, ` = *`, p.varGen.Current()) } } else if field.IsBytes() { if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, `[i] = make([]byte,`, p.varGen.Current(), `)`) p.P(`for j := 0; j < `, p.varGen.Current(), `; j++ {`) p.In() p.P(`this.`, fieldname, `[i][j] = byte(r.Intn(256))`) p.Out() p.P(`}`) p.Out() p.P(`}`) } else { p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = byte(r.Intn(256))`) p.Out() p.P(`}`) } } else if field.IsString() { val := fmt.Sprintf("randString%v(r)", p.localName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, val) p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, val) } else { p.P(p.varGen.Next(), `:= `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } else { typName := generator.GoTypeToName(goTyp) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, `[i] *= -1`) p.Out() p.P(`}`) } p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, ` *= -1`) p.Out() p.P(`}`) } } else { p.P(p.varGen.Next(), ` := `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(p.varGen.Current(), ` *= -1`) p.Out() p.P(`}`) } p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } } }
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 }
func (p *unmarshal) mapField(varName string, field *descriptor.FieldDescriptorProto) { switch field.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: p.P(`var `, varName, `temp uint64`) p.decodeFixed64(varName+"temp", "uint64") p.P(varName, ` := `, p.mathPkg.Use(), `.Float64frombits(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_FLOAT: p.P(`var `, varName, `temp uint32`) p.decodeFixed32(varName+"temp", "uint32") p.P(varName, ` := `, p.mathPkg.Use(), `.Float32frombits(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_INT64: p.P(`var `, varName, ` int64`) p.decodeVarint(varName, "int64") case descriptor.FieldDescriptorProto_TYPE_UINT64: p.P(`var `, varName, ` uint64`) p.decodeVarint(varName, "uint64") case descriptor.FieldDescriptorProto_TYPE_INT32: p.P(`var `, varName, ` int32`) p.decodeVarint(varName, "int32") case descriptor.FieldDescriptorProto_TYPE_FIXED64: p.P(`var `, varName, ` uint64`) p.decodeFixed64(varName, "uint64") case descriptor.FieldDescriptorProto_TYPE_FIXED32: p.P(`var `, varName, ` uint32`) p.decodeFixed32(varName, "uint32") case descriptor.FieldDescriptorProto_TYPE_BOOL: p.P(`var `, varName, `temp int`) p.decodeVarint(varName+"temp", "int") p.P(varName, ` := bool(`, varName, `temp != 0)`) case descriptor.FieldDescriptorProto_TYPE_STRING: p.P(`var stringLen`, varName, ` uint64`) p.decodeVarint("stringLen"+varName, "uint64") p.P(`intStringLen`, varName, ` := int(stringLen`, varName, `)`) p.P(`if intStringLen`, varName, ` < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postStringIndex`, varName, ` := iNdEx + intStringLen`, varName) p.P(`if postStringIndex`, varName, ` > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) cast, _ := p.GoType(nil, field) cast = strings.Replace(cast, "*", "", 1) p.P(varName, ` := `, cast, `(data[iNdEx:postStringIndex`, varName, `])`) p.P(`iNdEx = postStringIndex`, varName) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: p.P(`var mapmsglen int`) p.decodeVarint("mapmsglen", "int") p.P(`if mapmsglen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postmsgIndex := iNdEx + mapmsglen`) p.P(`if mapmsglen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`if postmsgIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) p.P(varName, ` := &`, msgname, `{}`) p.P(`if err := `, varName, `.Unmarshal(data[iNdEx:postmsgIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) p.P(`iNdEx = postmsgIndex`) case descriptor.FieldDescriptorProto_TYPE_BYTES: p.P(`var mapbyteLen uint64`) p.decodeVarint("mapbyteLen", "uint64") p.P(`intMapbyteLen := int(mapbyteLen)`) p.P(`if intMapbyteLen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postbytesIndex := iNdEx + intMapbyteLen`) p.P(`if postbytesIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) p.P(varName, ` := make([]byte, mapbyteLen)`) p.P(`copy(`, varName, `, data[iNdEx:postbytesIndex])`) p.P(`iNdEx = postbytesIndex`) case descriptor.FieldDescriptorProto_TYPE_UINT32: p.P(`var `, varName, ` uint32`) p.decodeVarint(varName, "uint32") case descriptor.FieldDescriptorProto_TYPE_ENUM: typName := p.TypeName(p.ObjectNamed(field.GetTypeName())) p.P(`var `, varName, ` `, typName) p.decodeVarint(varName, typName) case descriptor.FieldDescriptorProto_TYPE_SFIXED32: p.P(`var `, varName, ` int32`) p.decodeFixed32(varName, "int32") case descriptor.FieldDescriptorProto_TYPE_SFIXED64: p.P(`var `, varName, ` int64`) p.decodeFixed64(varName, "int64") case descriptor.FieldDescriptorProto_TYPE_SINT32: p.P(`var `, varName, `temp int32`) p.decodeVarint(varName+"temp", "int32") p.P(varName, `temp = int32((uint32(`, varName, `temp) >> 1) ^ uint32(((`, varName, `temp&1)<<31)>>31))`) p.P(varName, ` := int32(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_SINT64: p.P(`var `, varName, `temp uint64`) p.decodeVarint(varName+"temp", "uint64") p.P(varName, `temp = (`, varName, `temp >> 1) ^ uint64((int64(`, varName, `temp&1)<<63)>>63)`) p.P(varName, ` := int64(`, varName, `temp)`) } }