getterDefault finds the default value for the field to return from a getter, regardless of if it's a built in default or explicit from the source. Returns e.g. "nil", `""`, "Default_MessageType_FieldName"
(field *descriptor.FieldDescriptorProto, goMessageType string)
| 1691 | // getterDefault finds the default value for the field to return from a getter, |
| 1692 | // regardless of if it's a built in default or explicit from the source. Returns e.g. "nil", `""`, "Default_MessageType_FieldName" |
| 1693 | func (g *Generator) getterDefault(field *descriptor.FieldDescriptorProto, goMessageType string) string { |
| 1694 | if isRepeated(field) { |
| 1695 | return "nil" |
| 1696 | } |
| 1697 | if def := field.GetDefaultValue(); def != "" { |
| 1698 | defaultConstant := g.defaultConstantName(goMessageType, field.GetName()) |
| 1699 | if *field.Type != descriptor.FieldDescriptorProto_TYPE_BYTES { |
| 1700 | return defaultConstant |
| 1701 | } |
| 1702 | return "append([]byte(nil), " + defaultConstant + "...)" |
| 1703 | } |
| 1704 | switch *field.Type { |
| 1705 | case descriptor.FieldDescriptorProto_TYPE_BOOL: |
| 1706 | return "false" |
| 1707 | case descriptor.FieldDescriptorProto_TYPE_STRING: |
| 1708 | return `""` |
| 1709 | case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE, descriptor.FieldDescriptorProto_TYPE_BYTES: |
| 1710 | return "nil" |
| 1711 | case descriptor.FieldDescriptorProto_TYPE_ENUM: |
| 1712 | obj := g.ObjectNamed(field.GetTypeName()) |
| 1713 | var enum *EnumDescriptor |
| 1714 | if id, ok := obj.(*ImportedDescriptor); ok { |
| 1715 | // The enum type has been publicly imported. |
| 1716 | enum, _ = id.o.(*EnumDescriptor) |
| 1717 | } else { |
| 1718 | enum, _ = obj.(*EnumDescriptor) |
| 1719 | } |
| 1720 | if enum == nil { |
| 1721 | log.Printf("don't know how to generate getter for %s", field.GetName()) |
| 1722 | return "nil" |
| 1723 | } |
| 1724 | if len(enum.Value) == 0 { |
| 1725 | return "0 // empty enum" |
| 1726 | } |
| 1727 | first := enum.Value[0].GetName() |
| 1728 | return g.DefaultPackageName(obj) + enum.prefix() + first |
| 1729 | default: |
| 1730 | return "0" |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | // defaultConstantName builds the name of the default constant from the message |
| 1735 | // type name and the untouched field name, e.g. "Default_MessageType_FieldName" |
no test coverage detected