forked from hailey.at/cocoon
An atproto PDS written in Go

parse and validate record against atproto model (#5)

authored by juli.ee and committed by GitHub 573e83c0 3e890acb

Changed files
+29 -6
server
+29 -6
server/repo.go
··· 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "time" ··· 123 124 switch op.Type { 125 case OpTypeCreate: 126 - nc, err := r.PutRecord(context.TODO(), op.Collection+"/"+*op.Rkey, op.Record) 127 if err != nil { 128 return nil, err 129 } 130 - 131 - d, _ := data.MarshalCBOR(*op.Record) 132 entries = append(entries, models.Record{ 133 Did: urepo.Did, 134 CreatedAt: rm.clock.Next().String(), ··· 162 Type: to.StringPtr(OpTypeDelete.String()), 163 }) 164 case OpTypeUpdate: 165 - nc, err := r.UpdateRecord(context.TODO(), op.Collection+"/"+*op.Rkey, op.Record) 166 if err != nil { 167 return nil, err 168 } 169 - 170 - d, _ := data.MarshalCBOR(*op.Record) 171 entries = append(entries, models.Record{ 172 Did: urepo.Did, 173 CreatedAt: rm.clock.Next().String(),
··· 3 import ( 4 "bytes" 5 "context" 6 + "encoding/json" 7 "fmt" 8 "io" 9 "time" ··· 124 125 switch op.Type { 126 case OpTypeCreate: 127 + j, err := json.Marshal(*op.Record) 128 + if err != nil { 129 + return nil, err 130 + } 131 + out, err := data.UnmarshalJSON(j) 132 + if err != nil { 133 + return nil, err 134 + } 135 + mm := MarshalableMap(out) 136 + nc, err := r.PutRecord(context.TODO(), op.Collection+"/"+*op.Rkey, &mm) 137 if err != nil { 138 return nil, err 139 } 140 + d, err := data.MarshalCBOR(mm) 141 + if err != nil { 142 + return nil, err 143 + } 144 entries = append(entries, models.Record{ 145 Did: urepo.Did, 146 CreatedAt: rm.clock.Next().String(), ··· 174 Type: to.StringPtr(OpTypeDelete.String()), 175 }) 176 case OpTypeUpdate: 177 + j, err := json.Marshal(*op.Record) 178 + if err != nil { 179 + return nil, err 180 + } 181 + out, err := data.UnmarshalJSON(j) 182 + if err != nil { 183 + return nil, err 184 + } 185 + mm := MarshalableMap(out) 186 + nc, err := r.UpdateRecord(context.TODO(), op.Collection+"/"+*op.Rkey, &mm) 187 if err != nil { 188 return nil, err 189 } 190 + d, err := data.MarshalCBOR(mm) 191 + if err != nil { 192 + return nil, err 193 + } 194 entries = append(entries, models.Record{ 195 Did: urepo.Did, 196 CreatedAt: rm.clock.Next().String(),