|
| 1 | +// Package field provides methods to parse a field provided in a command with the format name:type |
| 2 | +package field |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/tendermint/starport/starport/pkg/multiformatname" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + TypeString = "string" |
| 13 | + TypeBool = "bool" |
| 14 | + TypeInt32 = "int32" |
| 15 | + TypeUint64 = "uint64" |
| 16 | +) |
| 17 | + |
| 18 | +// Field represents a field inside a structure for a component |
| 19 | +// it can a field contained in a type or inside the response of a query, etc... |
| 20 | +type Field struct { |
| 21 | + Name multiformatname.Name |
| 22 | + Datatype string |
| 23 | + DatatypeName string |
| 24 | +} |
| 25 | + |
| 26 | +// parseFields parses the provided fields, analyses the types and checks there is no duplicated field |
| 27 | +func ParseFields(fields []string, isForbiddenField func(string) error) ([]Field, error) { |
| 28 | + // Used to check duplicated field |
| 29 | + existingFields := make(map[string]bool) |
| 30 | + |
| 31 | + var parsedFields []Field |
| 32 | + for _, field := range fields { |
| 33 | + fieldSplit := strings.Split(field, ":") |
| 34 | + if len(fieldSplit) > 2 { |
| 35 | + return parsedFields, fmt.Errorf("invalid field format: %s, should be 'name' or 'name:type'", field) |
| 36 | + } |
| 37 | + |
| 38 | + name, err := multiformatname.NewName(fieldSplit[0]) |
| 39 | + if err != nil { |
| 40 | + return parsedFields, err |
| 41 | + } |
| 42 | + |
| 43 | + // Ensure the field name is not a Go reserved name, it would generate an incorrect code |
| 44 | + if err := isForbiddenField(name.LowerCamel); err != nil { |
| 45 | + return parsedFields, fmt.Errorf("%s can't be used as a field name: %s", name, err.Error()) |
| 46 | + } |
| 47 | + |
| 48 | + // Ensure the field is not duplicated |
| 49 | + if _, exists := existingFields[name.LowerCamel]; exists { |
| 50 | + return parsedFields, fmt.Errorf("the field %s is duplicated", name.Original) |
| 51 | + } |
| 52 | + existingFields[name.LowerCamel] = true |
| 53 | + |
| 54 | + // Parse the type if it is provided, otherwise string is used by defaut |
| 55 | + datatypeName, datatype := TypeString, TypeString |
| 56 | + isTypeSpecified := len(fieldSplit) == 2 |
| 57 | + if isTypeSpecified { |
| 58 | + acceptedTypes := map[string]string{ |
| 59 | + "string": TypeString, |
| 60 | + "bool": TypeBool, |
| 61 | + "int": TypeInt32, |
| 62 | + "uint": TypeUint64, |
| 63 | + } |
| 64 | + |
| 65 | + if t, ok := acceptedTypes[fieldSplit[1]]; ok { |
| 66 | + datatype = t |
| 67 | + datatypeName = fieldSplit[1] |
| 68 | + } else { |
| 69 | + return parsedFields, fmt.Errorf("the field type %s doesn't exist", fieldSplit[1]) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + parsedFields = append(parsedFields, Field{ |
| 74 | + Name: name, |
| 75 | + Datatype: datatype, |
| 76 | + DatatypeName: datatypeName, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return parsedFields, nil |
| 81 | +} |
0 commit comments