|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + covertMap = make(map[string]reflect.Type) |
| 12 | + covertMapR = make(map[reflect.Type]string) |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + Contents []Content |
| 17 | + RAWContents []RawContent |
| 18 | +) |
| 19 | + |
| 20 | +func (contents *Contents) UnmarshalJSON(bytes []byte) error { |
| 21 | + var raw RAWContents |
| 22 | + err := json.Unmarshal(bytes, &raw) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + result := make(Contents, 0, len(raw)) |
| 27 | + for _, content := range raw { |
| 28 | + r := covertMap[content.Type] |
| 29 | + if r == nil { |
| 30 | + result = append(result, |
| 31 | + ContentUnknown{ |
| 32 | + Type: content.Type, |
| 33 | + Value: content.Data, |
| 34 | + }) |
| 35 | + continue |
| 36 | + } |
| 37 | + i := reflect.New(r).Interface() |
| 38 | + if err := json.Unmarshal([]byte(content.Data), i); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + result = append(result, reflect.ValueOf(i).Elem().Interface().(Content)) |
| 42 | + } |
| 43 | + *contents = result |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (contents *Contents) MarshalJSON() ([]byte, error) { |
| 48 | + result := make(RAWContents, 0, len(*contents)) |
| 49 | + for _, content := range *contents { |
| 50 | + t := reflect.TypeOf(content) |
| 51 | + if t.Kind() == reflect.Ptr { |
| 52 | + t = t.Elem() |
| 53 | + } |
| 54 | + s := covertMapR[t] |
| 55 | + if s == "" { |
| 56 | + switch typedContent := content.(type) { |
| 57 | + case ContentUnknown: |
| 58 | + result = append(result, RawContent{ |
| 59 | + Type: typedContent.Type, |
| 60 | + Data: typedContent.Value, |
| 61 | + }) |
| 62 | + continue |
| 63 | + default: |
| 64 | + return nil, fmt.Errorf("unknown type %T", typedContent) |
| 65 | + } |
| 66 | + } |
| 67 | + data, err := json.Marshal(content) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + result = append(result, RawContent{ |
| 72 | + Type: s, |
| 73 | + Data: string(data), |
| 74 | + }) |
| 75 | + } |
| 76 | + return json.Marshal(result) |
| 77 | +} |
| 78 | + |
| 79 | +func (contents *Contents) String() string { |
| 80 | + var data []string |
| 81 | + for _, content := range *contents { |
| 82 | + data = append(data, content.String()) |
| 83 | + } |
| 84 | + return strings.Join(data, "") |
| 85 | +} |
| 86 | + |
| 87 | +type Content interface { |
| 88 | + fmt.Stringer |
| 89 | +} |
| 90 | + |
| 91 | +type RawContent struct { |
| 92 | + Type string `json:"type"` |
| 93 | + Data string `json:"data"` |
| 94 | +} |
| 95 | + |
| 96 | +var baseType = reflect.TypeOf((*Content)(nil)).Elem() |
| 97 | + |
| 98 | +func RegisterContent(key string, typeOf reflect.Type) { |
| 99 | + if _, ok := covertMap[key]; ok { |
| 100 | + panic("duplicate key " + key) |
| 101 | + } |
| 102 | + if !typeOf.Implements(baseType) { |
| 103 | + panic("type" + key + " not implements Content") |
| 104 | + } |
| 105 | + if typeOf.Kind() == reflect.Ptr { |
| 106 | + typeOf = typeOf.Elem() |
| 107 | + } |
| 108 | + covertMap[key] = typeOf |
| 109 | + covertMapR[typeOf] = key |
| 110 | +} |
| 111 | + |
| 112 | +type ContentUnknown struct { |
| 113 | + Type string `json:"type"` |
| 114 | + Value string `json:"value"` |
| 115 | +} |
| 116 | + |
| 117 | +func (c ContentUnknown) String() string { |
| 118 | + return fmt.Sprintf("unknown[type=%s]", c.Type) |
| 119 | +} |
0 commit comments