in types.go [101:176]
func (t *typeIFace) UnmarshalJSON(b []byte) error {
var typename string
err := json.Unmarshal(b, &typename)
if err == nil {
switch typename {
case "boolean":
t.Type = BooleanType{}
case "int":
t.Type = Int32Type{}
case "long":
t.Type = Int64Type{}
case "float":
t.Type = Float32Type{}
case "double":
t.Type = Float64Type{}
case "date":
t.Type = DateType{}
case "time":
t.Type = TimeType{}
case "timestamp":
t.Type = TimestampType{}
case "timestamptz":
t.Type = TimestampTzType{}
case "string":
t.Type = StringType{}
case "uuid":
t.Type = UUIDType{}
case "binary":
t.Type = BinaryType{}
default:
switch {
case strings.HasPrefix(typename, "fixed"):
matches := regexFromBrackets.FindStringSubmatch(typename)
if len(matches) != 2 {
return fmt.Errorf("%w: %s", ErrInvalidTypeString, typename)
}
n, _ := strconv.Atoi(matches[1])
t.Type = FixedType{len: n}
case strings.HasPrefix(typename, "decimal"):
matches := decimalRegex.FindStringSubmatch(typename)
if len(matches) != 3 {
return fmt.Errorf("%w: %s", ErrInvalidTypeString, typename)
}
prec, _ := strconv.Atoi(matches[1])
scale, _ := strconv.Atoi(matches[2])
t.Type = DecimalType{precision: prec, scale: scale}
default:
return fmt.Errorf("%w: unrecognized field type", ErrInvalidSchema)
}
}
return nil
}
aux := struct {
TypeName string `json:"type"`
}{}
if err = json.Unmarshal(b, &aux); err != nil {
return err
}
switch aux.TypeName {
case "list":
t.Type = &ListType{}
case "map":
t.Type = &MapType{}
case "struct":
t.Type = &StructType{}
default:
return fmt.Errorf("%w: %s", ErrInvalidTypeString, aux.TypeName)
}
return json.Unmarshal(b, t.Type)
}