1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Code generated by lexgen from lexicons/. DO NOT EDIT.
package infra
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bluesky-social/indigo/atproto/atdata"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/b64"
)
type Record interface {
NSID() string
}
func EncodeRecord(r Record) (map[string]any, error) {
b, err := json.Marshal(r)
if err != nil {
return nil, err
}
return atdata.UnmarshalJSON(b)
}
func decodeRecord(value map[string]any, nsid string, out any) error {
if got, _ := value["$type"].(string); got != nsid {
return fmt.Errorf("%w: expected $type %s, got %q", ErrInvalidRecord, nsid, got)
}
b, err := json.Marshal(b64.NormalizeJSON(value))
if err != nil {
return fmt.Errorf("%w: %v", ErrInvalidRecord, err)
}
if err := json.Unmarshal(b, out); err != nil {
return fmt.Errorf("%w: %v", ErrInvalidRecord, err)
}
if v, ok := out.(interface{ Validate() error }); ok {
return v.Validate()
}
return nil
}
func marshalOpen(v any, extra, absent map[string]json.RawMessage) ([]byte, error) {
b, err := json.Marshal(v)
if err != nil || len(extra)+len(absent) == 0 {
return b, err
}
fields := map[string]json.RawMessage{}
if err := json.Unmarshal(b, &fields); err != nil {
return nil, err
}
for k, raw := range absent {
if bytes.Equal(fields[k], raw) {
delete(fields, k)
}
}
for k, raw := range extra {
if _, ok := fields[k]; !ok {
fields[k] = raw
}
}
return json.Marshal(fields)
}
func unmarshalOpen(b []byte, v any) (extra, absent map[string]json.RawMessage, err error) {
if err := json.Unmarshal(b, v); err != nil {
return nil, nil, err
}
var in map[string]json.RawMessage
if err := json.Unmarshal(b, &in); err != nil {
return nil, nil, err
}
encoded, err := json.Marshal(v)
if err != nil {
return nil, nil, err
}
var kept map[string]json.RawMessage
if err := json.Unmarshal(encoded, &kept); err != nil {
return nil, nil, err
}
return missing(in, kept), missing(kept, in), nil
}
func missing(from, in map[string]json.RawMessage) map[string]json.RawMessage {
var out map[string]json.RawMessage
for k, raw := range from {
if _, ok := in[k]; ok {
continue
}
if out == nil {
out = map[string]json.RawMessage{}
}
out[k] = raw
}
return out
}
type EphemeralInfra struct {
Type string `json:"$type"`
Contracts []InfraContract `json:"contracts,omitempty"`
CreatedAt string `json:"createdAt"`
Description string `json:"description,omitempty"`
EndWhen string `json:"endWhen,omitempty"`
Status *EphemeralInfraStatus `json:"status,omitempty"`
TtlSeconds int64 `json:"ttlSeconds"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (EphemeralInfra) NSID() string {
return "computer.socialweb.ephemeralInfra"
}
func ParseEphemeralInfra(value map[string]any) (*EphemeralInfra, error) {
var out EphemeralInfra
if err := decodeRecord(value, "computer.socialweb.ephemeralInfra", &out); err != nil {
return nil, err
}
return &out, nil
}
func (v EphemeralInfra) MarshalJSON() ([]byte, error) {
type plain EphemeralInfra
if v.Type == "" {
v.Type = "computer.socialweb.ephemeralInfra"
}
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *EphemeralInfra) UnmarshalJSON(b []byte) error {
type plain EphemeralInfra
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = EphemeralInfra(p)
v.Extra = extra
v.absent = absent
return nil
}
type EphemeralInfraStatus struct {
Contracts []InfraContractStatus `json:"contracts,omitempty"`
EndReason string `json:"endReason,omitempty"`
EndedAt string `json:"endedAt,omitempty"`
ExpiresAt string `json:"expiresAt,omitempty"`
Phase string `json:"phase"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v EphemeralInfraStatus) MarshalJSON() ([]byte, error) {
type plain EphemeralInfraStatus
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *EphemeralInfraStatus) UnmarshalJSON(b []byte) error {
type plain EphemeralInfraStatus
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = EphemeralInfraStatus(p)
v.Extra = extra
v.absent = absent
return nil
}
type Infra struct {
Type string `json:"$type"`
Contracts []InfraContract `json:"contracts,omitempty"`
CreatedAt string `json:"createdAt"`
Description string `json:"description,omitempty"`
Index *InfraIndex `json:"index,omitempty"`
Status *InfraStatus `json:"status,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (Infra) NSID() string {
return "computer.socialweb.infra"
}
func ParseInfra(value map[string]any) (*Infra, error) {
var out Infra
if err := decodeRecord(value, "computer.socialweb.infra", &out); err != nil {
return nil, err
}
return &out, nil
}
func (v Infra) MarshalJSON() ([]byte, error) {
type plain Infra
if v.Type == "" {
v.Type = "computer.socialweb.infra"
}
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *Infra) UnmarshalJSON(b []byte) error {
type plain Infra
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = Infra(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraAgent struct {
EnvFile string `json:"envFile,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraAgent) MarshalJSON() ([]byte, error) {
type plain InfraAgent
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraAgent) UnmarshalJSON(b []byte) error {
type plain InfraAgent
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraAgent(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraAssociation struct {
FromGuest bool `json:"fromGuest,omitempty"`
RefFile string `json:"refFile,omitempty"`
Subject string `json:"subject,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraAssociation) MarshalJSON() ([]byte, error) {
type plain InfraAssociation
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraAssociation) UnmarshalJSON(b []byte) error {
type plain InfraAssociation
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraAssociation(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraContract struct {
Agent *InfraAgent `json:"agent,omitempty"`
Association *InfraAssociation `json:"association,omitempty"`
BidderDids []string `json:"bidderDids,omitempty"`
ComputeKind string `json:"computeKind,omitempty"`
DenyBidderDids []string `json:"denyBidderDids,omitempty"`
GuestTools []string `json:"guestTools,omitempty"`
Name string `json:"name"`
Policy string `json:"policy,omitempty"`
PolicyArgs string `json:"policyArgs,omitempty"`
ReleaseRequestedAt string `json:"releaseRequestedAt,omitempty"`
SSHAuthorizedKeys []string `json:"sshAuthorizedKeys,omitempty"`
State string `json:"state"`
Tunnels []InfraTunnel `json:"tunnels,omitempty"`
UserData string `json:"userData,omitempty"`
VMName string `json:"vmName,omitempty"`
VMReadyTimeoutSec int64 `json:"vmReadyTimeoutSec,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraContract) MarshalJSON() ([]byte, error) {
type plain InfraContract
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraContract) UnmarshalJSON(b []byte) error {
type plain InfraContract
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraContract(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraContractStatus struct {
Message string `json:"message,omitempty"`
Name string `json:"name"`
Phase string `json:"phase"`
Receipt string `json:"receipt,omitempty"`
Winner string `json:"winner,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraContractStatus) MarshalJSON() ([]byte, error) {
type plain InfraContractStatus
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraContractStatus) UnmarshalJSON(b []byte) error {
type plain InfraContractStatus
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraContractStatus(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraIndex struct {
Live []InfraIndexEntry `json:"live,omitempty"`
RecentEnded []InfraIndexEnded `json:"recentEnded,omitempty"`
UpdatedAt string `json:"updatedAt"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraIndex) MarshalJSON() ([]byte, error) {
type plain InfraIndex
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraIndex) UnmarshalJSON(b []byte) error {
type plain InfraIndex
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraIndex(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraIndexEnded struct {
Collection string `json:"collection"`
EndReason string `json:"endReason,omitempty"`
EndedAt string `json:"endedAt"`
Rkey string `json:"rkey"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraIndexEnded) MarshalJSON() ([]byte, error) {
type plain InfraIndexEnded
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraIndexEnded) UnmarshalJSON(b []byte) error {
type plain InfraIndexEnded
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraIndexEnded(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraIndexEntry struct {
AddedAt string `json:"addedAt"`
Collection string `json:"collection"`
CreatedAt string `json:"createdAt"`
ExpiresAt string `json:"expiresAt,omitempty"`
Rkey string `json:"rkey"`
Writer string `json:"writer,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraIndexEntry) MarshalJSON() ([]byte, error) {
type plain InfraIndexEntry
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraIndexEntry) UnmarshalJSON(b []byte) error {
type plain InfraIndexEntry
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraIndexEntry(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraShell struct {
EnvFile string `json:"envFile,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraShell) MarshalJSON() ([]byte, error) {
type plain InfraShell
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraShell) UnmarshalJSON(b []byte) error {
type plain InfraShell
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraShell(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraStatus struct {
Contracts []InfraContractStatus `json:"contracts,omitempty"`
EndReason string `json:"endReason,omitempty"`
EndedAt string `json:"endedAt,omitempty"`
Phase string `json:"phase"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraStatus) MarshalJSON() ([]byte, error) {
type plain InfraStatus
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraStatus) UnmarshalJSON(b []byte) error {
type plain InfraStatus
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraStatus(p)
v.Extra = extra
v.absent = absent
return nil
}
type InfraTunnel struct {
Port int64 `json:"port"`
Server string `json:"server,omitempty"`
Shell *InfraShell `json:"shell,omitempty"`
Tag string `json:"tag"`
Transport string `json:"transport,omitempty"`
Extra map[string]json.RawMessage `json:"-"`
absent map[string]json.RawMessage
}
func (v InfraTunnel) MarshalJSON() ([]byte, error) {
type plain InfraTunnel
return marshalOpen(plain(v), v.Extra, v.absent)
}
func (v *InfraTunnel) UnmarshalJSON(b []byte) error {
type plain InfraTunnel
var p plain
extra, absent, err := unmarshalOpen(b, &p)
if err != nil {
return err
}
*v = InfraTunnel(p)
v.Extra = extra
v.absent = absent
return nil
}