| 1 | package schema |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "reflect" |
| 8 | "regexp" |
| 9 | |
| 10 | "git.sr.ht/~sircmpwn/go-bare" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | intType = reflect.TypeOf(bare.Int(0)) |
| 15 | uintType = reflect.TypeOf(bare.Uint(0)) |
| 16 | ) |
| 17 | |
| 18 | // Given a pointer to a value, returns the BARE schema language representation |
| 19 | // for that value type. |
| 20 | // |
| 21 | // var example string |
| 22 | // schema.SchemaFor(&example); // "string" |
| 23 | // |
| 24 | // Given a struct type, if the "bare" tag is found on its fields, it will be |
| 25 | // used as the field name in the generated schema. |
| 26 | func SchemaFor(val interface{}) (string, error) { |
| 27 | t := reflect.TypeOf(val) |
| 28 | if t.Kind() == reflect.Ptr { |
| 29 | t = t.Elem() |
| 30 | } else { |
| 31 | return "", errors.New("Expected val to be pointer type") |
| 32 | } |
| 33 | return SchemaForType(t) |
| 34 | } |
| 35 | |
| 36 | // Given a reflect.Type, returns the BARE schema language representation for |
| 37 | // that type. See SchemaFor for details. |
| 38 | func SchemaForType(t reflect.Type) (string, error) { |
| 39 | // TODO: Implement user-defined types for unparsing schemas from |
| 40 | if t.Kind() == reflect.Ptr { |
| 41 | schema, err := SchemaForType(t.Elem()) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | return fmt.Sprintf("optional<%s>", schema), nil |
| 46 | } |
| 47 | |
| 48 | switch t { |
| 49 | case intType: |
| 50 | return "int", nil |
| 51 | case uintType: |
| 52 | return "uint", nil |
| 53 | } |
| 54 | |
| 55 | switch t.Kind() { |
| 56 | case reflect.Uint8: |
| 57 | return "u8", nil |
| 58 | case reflect.Uint16: |
| 59 | return "u16", nil |
| 60 | case reflect.Uint32: |
| 61 | return "u32", nil |
| 62 | case reflect.Uint64: |
| 63 | return "u64", nil |
| 64 | case reflect.Uint: |
| 65 | return "uint", nil |
| 66 | case reflect.Int8: |
| 67 | return "i8", nil |
| 68 | case reflect.Int16: |
| 69 | return "i16", nil |
| 70 | case reflect.Int32: |
| 71 | return "i32", nil |
| 72 | case reflect.Int64: |
| 73 | return "i64", nil |
| 74 | case reflect.Int: |
| 75 | return "int", nil |
| 76 | case reflect.Float32: |
| 77 | return "f32", nil |
| 78 | case reflect.Float64: |
| 79 | return "f64", nil |
| 80 | case reflect.Bool: |
| 81 | return "bool", nil |
| 82 | case reflect.String: |
| 83 | return "string", nil |
| 84 | case reflect.Struct: |
| 85 | return schemaForStruct(t) |
| 86 | default: |
| 87 | return "", &bare.UnsupportedTypeError{t} |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | var tagFieldNameRE = regexp.MustCompile(`"[a-z][A-Za-z0-9]*"`) |
| 92 | |
| 93 | func schemaForStruct(t reflect.Type) (string, error) { |
| 94 | buf := bytes.NewBufferString("{\n") |
| 95 | for i := 0; i < t.NumField(); i++ { |
| 96 | field := t.Field(i) |
| 97 | schema, err := SchemaForType(field.Type) |
| 98 | if err != nil { |
| 99 | return "", err |
| 100 | } |
| 101 | // TODO: Convert Go names into valid schema names |
| 102 | name := field.Name |
| 103 | if tag, ok := field.Tag.Lookup("bare"); ok { |
| 104 | name = tagFieldNameRE.FindString(tag) |
| 105 | if name == "" { |
| 106 | name = field.Name |
| 107 | } |
| 108 | } |
| 109 | buf.WriteString(fmt.Sprintf("\t%s: %s\n", name, schema)) |
| 110 | } |
| 111 | buf.WriteString("}") |
| 112 | return buf.String(), nil |
| 113 | } |