repr.go 23 KB
Newer Older
1 2 3 4 5 6 7
package bindnode

import (
	"fmt"
	"reflect"
	"strings"

tavit ohanian's avatar
tavit ohanian committed
8
	ld "gitlab.dms3.io/ld/go-ld-prime"
tavit ohanian's avatar
tavit ohanian committed
9 10
	basicnode "gitlab.dms3.io/ld/go-ld-prime/node/basic"
	"gitlab.dms3.io/ld/go-ld-prime/schema"
11 12
)

tavit ohanian's avatar
tavit ohanian committed
13
func reprNode(node ld.Node) ld.Node {
14 15 16
	if node, ok := node.(schema.TypedNode); ok {
		return node.Representation()
	}
tavit ohanian's avatar
tavit ohanian committed
17
	// ld.Absent and ld.Null are not typed.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	// TODO: is this a problem? surely a typed struct's fields are always
	// typed, even when absent or null.
	return node
}

func reprStrategy(typ schema.Type) interface{} {
	switch typ := typ.(type) {
	case *schema.TypeStruct:
		return typ.RepresentationStrategy()
	case *schema.TypeUnion:
		return typ.RepresentationStrategy()
	}
	return nil
}

type _prototypeRepr _prototype

tavit ohanian's avatar
tavit ohanian committed
35
func (w *_prototypeRepr) NewBuilder() ld.NodeBuilder {
36 37 38 39 40 41 42 43
	return &_builderRepr{_assemblerRepr{
		schemaType: w.schemaType,
		val:        reflect.New(w.goType).Elem(),
	}}
}

type _nodeRepr _node

tavit ohanian's avatar
tavit ohanian committed
44
func (w *_nodeRepr) Kind() ld.Kind {
45 46
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Stringjoin:
tavit ohanian's avatar
tavit ohanian committed
47
		return ld.Kind_String
48
	case schema.StructRepresentation_Map:
tavit ohanian's avatar
tavit ohanian committed
49
		return ld.Kind_Map
50
	case schema.StructRepresentation_Tuple:
tavit ohanian's avatar
tavit ohanian committed
51
		return ld.Kind_List
52
	case schema.UnionRepresentation_Keyed:
tavit ohanian's avatar
tavit ohanian committed
53
		return ld.Kind_Map
54 55 56 57 58
	case schema.UnionRepresentation_Kinded:
		haveIdx := int(w.val.FieldByName("Index").Int())
		mtyp := w.schemaType.(*schema.TypeUnion).Members()[haveIdx]
		return mtyp.TypeKind().ActsLike()
	case schema.UnionRepresentation_Stringprefix:
tavit ohanian's avatar
tavit ohanian committed
59
		return ld.Kind_String
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
	case nil:
		return (*_node)(w).Kind()
	default:
		panic(fmt.Sprintf("TODO Kind: %T", stg))
	}
}

func outboundMappedKey(stg schema.StructRepresentation_Map, key string) string {
	// TODO: why doesn't stg just allow us to "get" by the key string?
	field := schema.SpawnStructField(key, "", false, false)
	mappedKey := stg.GetFieldKey(field)
	return mappedKey
}

func inboundMappedKey(typ *schema.TypeStruct, stg schema.StructRepresentation_Map, key string) string {
	// TODO: can't do a "reverse" lookup... needs better API probably.
	fields := typ.Fields()
	for _, field := range fields {
		mappedKey := stg.GetFieldKey(field)
		if key == mappedKey {
			// println(key, "rev-mapped to", field.Name())
			return field.Name()
		}
	}
	// println(key, "had no mapping")
	return key // fallback to the same key
}

func outboundMappedType(stg schema.UnionRepresentation_Keyed, key string) string {
	// TODO: why doesn't stg just allow us to "get" by the key string?
	typ := schema.SpawnBool(schema.TypeName(key))
	mappedKey := stg.GetDiscriminant(typ)
	return mappedKey
}

func inboundMappedType(typ *schema.TypeUnion, stg schema.UnionRepresentation_Keyed, key string) string {
	// TODO: can't do a "reverse" lookup... needs better API probably.
	for _, member := range typ.Members() {
		mappedKey := stg.GetDiscriminant(member)
		if key == mappedKey {
			// println(key, "rev-mapped to", field.Name())
			return member.Name().String()
		}
	}
	// println(key, "had no mapping")
	return key // fallback to the same key
}

tavit ohanian's avatar
tavit ohanian committed
108
func (w *_nodeRepr) asKinded(stg schema.UnionRepresentation_Kinded, kind ld.Kind) *_nodeRepr {
109 110 111 112 113 114 115 116 117 118 119 120 121 122
	name := stg.GetMember(kind)
	members := w.schemaType.(*schema.TypeUnion).Members()
	for _, member := range members {
		if member.Name() != name {
			continue
		}
		w2 := *w
		w2.val = w.val.FieldByName("Value").Elem()
		w2.schemaType = member
		return &w2
	}
	return nil
}

tavit ohanian's avatar
tavit ohanian committed
123
func (w *_nodeRepr) LookupByString(key string) (ld.Node, error) {
124
	if stg, ok := reprStrategy(w.schemaType).(schema.UnionRepresentation_Kinded); ok {
tavit ohanian's avatar
tavit ohanian committed
125
		w = w.asKinded(stg, ld.Kind_Map)
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
	}
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		revKey := inboundMappedKey(w.schemaType.(*schema.TypeStruct), stg, key)
		v, err := (*_node)(w).LookupByString(revKey)
		if err != nil {
			return nil, err
		}
		return reprNode(v), nil
	case schema.UnionRepresentation_Keyed:
		revKey := inboundMappedType(w.schemaType.(*schema.TypeUnion), stg, key)
		v, err := (*_node)(w).LookupByString(revKey)
		if err != nil {
			return nil, err
		}
		return reprNode(v), nil
	case nil:
		v, err := (*_node)(w).LookupByString(key)
		if err != nil {
			return nil, err
		}
		return reprNode(v), nil
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
153
func (w *_nodeRepr) LookupByIndex(idx int64) (ld.Node, error) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Tuple:
		fields := w.schemaType.(*schema.TypeStruct).Fields()
		field := fields[idx]
		v, err := (*_node)(w).LookupByString(field.Name())
		if err != nil {
			return nil, err
		}
		return reprNode(v), nil
	case nil:
		v, err := (*_node)(w).LookupByIndex(idx)
		if err != nil {
			return nil, err
		}
		return reprNode(v), nil
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
174
func (w *_nodeRepr) LookupBySegment(seg ld.PathSegment) (ld.Node, error) {
175
	switch w.Kind() {
tavit ohanian's avatar
tavit ohanian committed
176
	case ld.Kind_Map:
177
		return w.LookupByString(seg.String())
tavit ohanian's avatar
tavit ohanian committed
178
	case ld.Kind_List:
179 180 181 182 183 184
		idx, err := seg.Index()
		if err != nil {
			return nil, err
		}
		return w.LookupByIndex(idx)
	}
tavit ohanian's avatar
tavit ohanian committed
185
	return nil, ld.ErrWrongKind{
186 187 188 189 190 191
		TypeName:   w.schemaType.Name().String(),
		MethodName: "LookupBySegment",
		// TODO
	}
}

tavit ohanian's avatar
tavit ohanian committed
192
func (w *_nodeRepr) LookupByNode(key ld.Node) (ld.Node, error) {
193
	switch w.Kind() {
tavit ohanian's avatar
tavit ohanian committed
194
	case ld.Kind_Map:
195 196 197 198 199
		s, err := key.AsString()
		if err != nil {
			return nil, err
		}
		return w.LookupByString(s)
tavit ohanian's avatar
tavit ohanian committed
200
	case ld.Kind_List:
201 202 203 204 205 206
		i, err := key.AsInt()
		if err != nil {
			return nil, err
		}
		return w.LookupByIndex(i)
	}
tavit ohanian's avatar
tavit ohanian committed
207
	return nil, ld.ErrWrongKind{
208 209 210 211 212 213
		TypeName:   w.schemaType.Name().String(),
		MethodName: "LookupByNode",
		// TODO
	}
}

tavit ohanian's avatar
tavit ohanian committed
214
func (w *_nodeRepr) MapIterator() ld.MapIterator {
215
	if stg, ok := reprStrategy(w.schemaType).(schema.UnionRepresentation_Kinded); ok {
tavit ohanian's avatar
tavit ohanian committed
216
		w = w.asKinded(stg, ld.Kind_Map)
217 218 219 220 221 222 223 224 225
	}
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		itr := (*_node)(w).MapIterator().(*_structIterator)
		itr.reprEnd = int(w.lengthMinusTrailingAbsents())
		return (*_structIteratorRepr)(itr)
	case schema.UnionRepresentation_Keyed:
		itr := (*_node)(w).MapIterator().(*_unionIterator)
		return (*_unionIteratorRepr)(itr)
226 227
	case nil:
		return (*_node)(w).MapIterator()
228 229 230 231 232
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
233
func (w *_nodeRepr) ListIterator() ld.ListIterator {
234 235 236
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).ListIterator()
	}
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
	return nil
}

func (w *_nodeRepr) lengthMinusTrailingAbsents() int64 {
	fields := w.schemaType.(*schema.TypeStruct).Fields()
	for i := len(fields) - 1; i >= 0; i-- {
		field := fields[i]
		if !field.IsOptional() || !w.val.Field(i).IsNil() {
			return int64(i + 1)
		}
	}
	return 0
}

func (w *_nodeRepr) Length() int64 {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Stringjoin:
		return -1
	case schema.StructRepresentation_Map:
		return w.lengthMinusTrailingAbsents()
	case schema.StructRepresentation_Tuple:
		return w.lengthMinusTrailingAbsents()
	case schema.UnionRepresentation_Keyed:
		return (*_node)(w).Length()
	case schema.UnionRepresentation_Kinded:
		w = w.asKinded(stg, w.Kind())
		// continues below
	case nil:
		// continues below
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
	return (*_node)(w).Length()
}

func (w *_nodeRepr) IsAbsent() bool {
273 274 275
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).IsAbsent()
	}
276 277 278 279
	return false
}

func (w *_nodeRepr) IsNull() bool {
280 281 282
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).IsNull()
	}
283 284 285 286
	return false
}

func (w *_nodeRepr) AsBool() (bool, error) {
287 288 289
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).AsBool()
	}
tavit ohanian's avatar
tavit ohanian committed
290
	return false, ld.ErrWrongKind{
291 292
		TypeName:        w.schemaType.Name().String(),
		MethodName:      "AsBool",
tavit ohanian's avatar
tavit ohanian committed
293
		AppropriateKind: ld.KindSet_JustBool,
294
		// TODO
295 296 297 298
	}
}

func (w *_nodeRepr) AsInt() (int64, error) {
299 300 301
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).AsInt()
	}
tavit ohanian's avatar
tavit ohanian committed
302
	return 0, ld.ErrWrongKind{
303 304
		TypeName:        w.schemaType.Name().String(),
		MethodName:      "AsInt",
tavit ohanian's avatar
tavit ohanian committed
305
		AppropriateKind: ld.KindSet_JustInt,
306
		// TODO
307 308 309 310
	}
}

func (w *_nodeRepr) AsFloat() (float64, error) {
311 312 313
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).AsFloat()
	}
tavit ohanian's avatar
tavit ohanian committed
314
	return 0, ld.ErrWrongKind{
315 316
		TypeName:        w.schemaType.Name().String(),
		MethodName:      "AsFloat",
tavit ohanian's avatar
tavit ohanian committed
317
		AppropriateKind: ld.KindSet_JustFloat,
318
		// TODO
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
	}
}

func (w *_nodeRepr) AsString() (string, error) {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Stringjoin:
		var b strings.Builder
		itr := (*_node)(w).MapIterator()
		for !itr.Done() {
			_, v, err := itr.Next()
			if err != nil {
				return "", err
			}
			s, err := reprNode(v).AsString()
			if err != nil {
				return "", err
			}
			if b.Len() > 0 {
				b.WriteString(stg.GetDelim())
			}
			b.WriteString(s)
		}
		return b.String(), nil
	case schema.UnionRepresentation_Stringprefix:
		haveIdx := int(w.val.FieldByName("Index").Int())
		mtyp := w.schemaType.(*schema.TypeUnion).Members()[haveIdx]

		w2 := *w
		w2.val = w.val.FieldByName("Value").Elem()
		w2.schemaType = mtyp
		s, err := w2.AsString()
		if err != nil {
			return "", err
		}

		name := stg.GetDiscriminant(mtyp)
		return name + stg.GetDelim() + s, nil
	case schema.UnionRepresentation_Kinded:
tavit ohanian's avatar
tavit ohanian committed
357
		w = w.asKinded(stg, ld.Kind_String)
358 359 360 361 362 363 364 365 366 367
		// continues below
	case nil:
		// continues below
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
	return (*_node)(w).AsString()
}

func (w *_nodeRepr) AsBytes() ([]byte, error) {
368 369 370
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).AsBytes()
	}
tavit ohanian's avatar
tavit ohanian committed
371
	return nil, ld.ErrWrongKind{
372 373
		TypeName:        w.schemaType.Name().String(),
		MethodName:      "AsBytes",
tavit ohanian's avatar
tavit ohanian committed
374
		AppropriateKind: ld.KindSet_JustBytes,
375
		// TODO
376 377 378
	}
}

tavit ohanian's avatar
tavit ohanian committed
379
func (w *_nodeRepr) AsLink() (ld.Link, error) {
380 381 382
	if reprStrategy(w.schemaType) == nil {
		return (*_node)(w).AsLink()
	}
tavit ohanian's avatar
tavit ohanian committed
383
	return nil, ld.ErrWrongKind{
384 385
		TypeName:        w.schemaType.Name().String(),
		MethodName:      "AsLink",
tavit ohanian's avatar
tavit ohanian committed
386
		AppropriateKind: ld.KindSet_JustLink,
387
		// TODO
388 389 390
	}
}

tavit ohanian's avatar
tavit ohanian committed
391
func (w *_nodeRepr) Prototype() ld.NodePrototype {
392 393 394 395 396 397 398 399 400 401 402 403 404
	panic("TODO: Prototype")
}

type _builderRepr struct {
	_assemblerRepr
}

// TODO: returning a repr node here is probably good, but there's a gotcha: one
// can go from a typed node to a repr node via the Representation method, but
// not the other way. That's probably why codegen returns a typed node here.
// The solution might be to add a way to go from the repr node to its parent
// typed node.

tavit ohanian's avatar
tavit ohanian committed
405
func (w *_builderRepr) Build() ld.Node {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	// TODO: see the notes above.
	// return &_nodeRepr{schemaType: w.schemaType, val: w.val}
	return &_node{schemaType: w.schemaType, val: w.val}
}

func (w *_builderRepr) Reset() {
	panic("TODO: Reset")
}

type _assemblerRepr struct {
	schemaType schema.Type
	val        reflect.Value // non-pointer
	finish     func() error

	nullable bool
}

tavit ohanian's avatar
tavit ohanian committed
423
func assemblerRepr(am ld.NodeAssembler) *_assemblerRepr {
424 425 426
	return (*_assemblerRepr)(am.(*_assembler))
}

tavit ohanian's avatar
tavit ohanian committed
427
func (w *_assemblerRepr) asKinded(stg schema.UnionRepresentation_Kinded, kind ld.Kind) *_assemblerRepr {
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
	name := stg.GetMember(kind)
	members := w.schemaType.(*schema.TypeUnion).Members()
	for idx, member := range members {
		if member.Name() != name {
			continue
		}
		w2 := *w
		goType := inferGoType(member) // TODO: do this upfront
		w2.val = reflect.New(goType).Elem()
		w2.schemaType = member

		// Layer a new finish func on top, to set Index/Value.
		w2.finish = func() error {
			if w.finish != nil {
				if err := w.finish(); err != nil {
					return err
				}
			}
			w.val.FieldByName("Index").SetInt(int64(idx))
			w.val.FieldByName("Value").Set(w2.val)
			return nil
		}
		return &w2
	}
	return nil
}

tavit ohanian's avatar
tavit ohanian committed
455
func (w *_assemblerRepr) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
456
	if stg, ok := reprStrategy(w.schemaType).(schema.UnionRepresentation_Kinded); ok {
tavit ohanian's avatar
tavit ohanian committed
457
		w = w.asKinded(stg, ld.Kind_Map)
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	}
	asm, err := (*_assembler)(w).BeginMap(sizeHint)
	if err != nil {
		return nil, err
	}
	switch asm := asm.(type) {
	case *_structAssembler:
		return (*_structAssemblerRepr)(asm), nil
	case *_mapAssembler:
		return (*_mapAssemblerRepr)(asm), nil
	case *_unionAssembler:
		return (*_unionAssemblerRepr)(asm), nil
	default:
		panic(fmt.Sprintf("%T", asm))
	}
}

tavit ohanian's avatar
tavit ohanian committed
475
func (w *_assemblerRepr) BeginList(sizeHint int64) (ld.ListAssembler, error) {
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Tuple:
		asm, err := (*_assembler)(w).BeginMap(sizeHint)
		if err != nil {
			return nil, err
		}
		return (*_listStructAssemblerRepr)(asm.(*_structAssembler)), nil
	case nil:
		asm, err := (*_assembler)(w).BeginList(sizeHint)
		if err != nil {
			return nil, err
		}
		return (*_listAssemblerRepr)(asm.(*_listAssembler)), nil
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

func (w *_assemblerRepr) AssignNull() error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignNull()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

503 504 505 506 507 508 509
func (w *_assemblerRepr) AssignBool(b bool) error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignBool(b)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
510 511 512
}

func (w *_assemblerRepr) AssignInt(i int64) error {
513 514 515 516 517 518
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignInt(i)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
519 520
}

521 522 523 524 525 526 527
func (w *_assemblerRepr) AssignFloat(f float64) error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignFloat(f)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
}

func (w *_assemblerRepr) AssignString(s string) error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Stringjoin:
		fields := w.schemaType.(*schema.TypeStruct).Fields()
		parts := strings.Split(s, stg.GetDelim())
		if len(parts) != len(fields) {
			panic("TODO: len mismatch")
		}
		mapAsm, err := (*_assembler)(w).BeginMap(-1)
		if err != nil {
			return err
		}
		for i, field := range fields {
			entryAsm, err := mapAsm.AssembleEntry(field.Name())
			if err != nil {
				return err
			}
547
			entryAsm = assemblerRepr(entryAsm)
548 549 550 551 552 553
			if err := entryAsm.AssignString(parts[i]); err != nil {
				return err
			}
		}
		return mapAsm.Finish()
	case schema.UnionRepresentation_Kinded:
tavit ohanian's avatar
tavit ohanian committed
554
		name := stg.GetMember(ld.Kind_String)
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
		members := w.schemaType.(*schema.TypeUnion).Members()
		for idx, member := range members {
			if member.Name() != name {
				continue
			}
			w.val.FieldByName("Index").SetInt(int64(idx))
			w.val.FieldByName("Value").Set(reflect.ValueOf(s))
			return nil
		}
		panic("TODO: GetMember result is missing?")
	case schema.UnionRepresentation_Stringprefix:
		parts := strings.SplitN(s, stg.GetDelim(), 2)
		if len(parts) != 2 {
			panic("TODO: bad format")
		}
		name, value := parts[0], parts[1]
		members := w.schemaType.(*schema.TypeUnion).Members()
		for idx, member := range members {
			if stg.GetDiscriminant(member) != name {
				continue
			}

			w2 := *w
			goType := inferGoType(member) // TODO: do this upfront
			w2.val = reflect.New(goType).Elem()
			w2.schemaType = member
			w2.finish = func() error {
				if w.finish != nil {
					if err := w.finish(); err != nil {
						return err
					}
				}
				w.val.FieldByName("Index").SetInt(int64(idx))
				w.val.FieldByName("Value").Set(w2.val)
				return nil
			}

			return w2.AssignString(value)
		}
		panic("TODO: GetMember result is missing?")
	case nil:
		return (*_assembler)(w).AssignString(s)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

func (w *_assemblerRepr) AssignBytes(p []byte) error {
603 604 605 606 607 608
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignBytes(p)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
609 610
}

tavit ohanian's avatar
tavit ohanian committed
611
func (w *_assemblerRepr) AssignLink(link ld.Link) error {
612 613 614 615 616 617
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignLink(link)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
618 619
}

tavit ohanian's avatar
tavit ohanian committed
620
func (w *_assemblerRepr) AssignNode(node ld.Node) error {
621 622 623 624 625 626
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_assembler)(w).AssignNode(node)
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
627 628
}

tavit ohanian's avatar
tavit ohanian committed
629
func (w *_assemblerRepr) Prototype() ld.NodePrototype {
630 631 632 633 634
	panic("TODO: Assembler.Prototype")
}

type _structAssemblerRepr _structAssembler

tavit ohanian's avatar
tavit ohanian committed
635
func (w *_structAssemblerRepr) AssembleKey() ld.NodeAssembler {
636 637 638 639 640 641 642 643
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		return (*_structAssembler)(w).AssembleKey()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
644
func (w *_structAssemblerRepr) AssembleValue() ld.NodeAssembler {
645 646 647 648 649 650 651
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		key := w.curKey.val.String()
		revKey := inboundMappedKey(w.schemaType, stg, key)
		w.curKey.val.SetString(revKey)

		valAsm := (*_structAssembler)(w).AssembleValue()
652
		valAsm = assemblerRepr(valAsm)
653 654 655 656 657 658
		return valAsm
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
659
func (w *_structAssemblerRepr) AssembleEntry(k string) (ld.NodeAssembler, error) {
660 661 662 663 664 665 666 667 668 669 670
	if err := w.AssembleKey().AssignString(k); err != nil {
		return nil, err
	}
	am := w.AssembleValue()
	return am, nil
}

func (w *_structAssemblerRepr) Finish() error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		err := (*_structAssembler)(w).Finish()
tavit ohanian's avatar
tavit ohanian committed
671
		if err, ok := err.(ld.ErrMissingRequiredField); ok {
672 673 674 675 676 677 678 679 680 681 682 683 684
			for i, name := range err.Missing {
				serial := outboundMappedKey(stg, name)
				if serial != name {
					err.Missing[i] += fmt.Sprintf(" (serial:%q)", serial)
				}
			}
		}
		return err
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
685
func (w *_structAssemblerRepr) KeyPrototype() ld.NodePrototype {
686 687 688
	panic("TODO")
}

tavit ohanian's avatar
tavit ohanian committed
689
func (w *_structAssemblerRepr) ValuePrototype(k string) ld.NodePrototype {
690 691 692 693 694
	panic("TODO: struct ValuePrototype")
}

type _mapAssemblerRepr _mapAssembler

tavit ohanian's avatar
tavit ohanian committed
695
func (w *_mapAssemblerRepr) AssembleKey() ld.NodeAssembler {
696 697 698 699 700 701 702 703 704
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		asm := (*_mapAssembler)(w).AssembleKey()
		return (*_assemblerRepr)(asm.(*_assembler))
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
705
func (w *_mapAssemblerRepr) AssembleValue() ld.NodeAssembler {
706 707 708 709 710 711 712 713 714
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		asm := (*_mapAssembler)(w).AssembleValue()
		return (*_assemblerRepr)(asm.(*_assembler))
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
715
func (w *_mapAssemblerRepr) AssembleEntry(k string) (ld.NodeAssembler, error) {
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	if err := w.AssembleKey().AssignString(k); err != nil {
		return nil, err
	}
	am := w.AssembleValue()
	return am, nil
}

func (w *_mapAssemblerRepr) Finish() error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case nil:
		return (*_mapAssembler)(w).Finish()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
732
func (w *_mapAssemblerRepr) KeyPrototype() ld.NodePrototype {
733 734 735
	panic("TODO")
}

tavit ohanian's avatar
tavit ohanian committed
736
func (w *_mapAssemblerRepr) ValuePrototype(k string) ld.NodePrototype {
737 738 739 740 741
	panic("TODO: struct ValuePrototype")
}

type _listStructAssemblerRepr _structAssembler

tavit ohanian's avatar
tavit ohanian committed
742
func (w *_listStructAssemblerRepr) AssembleValue() ld.NodeAssembler {
743 744 745 746 747 748 749 750 751 752
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Tuple:
		fields := w.schemaType.Fields()
		field := fields[w.nextIndex]
		w.nextIndex++

		entryAsm, err := (*_structAssembler)(w).AssembleEntry(field.Name())
		if err != nil {
			panic(err) // TODO: probably return an assembler that always errors?
		}
753
		entryAsm = assemblerRepr(entryAsm)
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
		return entryAsm
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

func (w *_listStructAssemblerRepr) Finish() error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Tuple:
		return (*_structAssembler)(w).Finish()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
769
func (w *_listStructAssemblerRepr) ValuePrototype(idx int64) ld.NodePrototype {
770 771 772 773 774 775
	panic("TODO: list ValuePrototype")
}

// Note that lists do not have any representation strategy right now.
type _listAssemblerRepr _listAssembler

tavit ohanian's avatar
tavit ohanian committed
776
func (w *_listAssemblerRepr) AssembleValue() ld.NodeAssembler {
777 778 779 780 781 782 783 784
	asm := (*_listAssembler)(w).AssembleValue()
	return (*_assemblerRepr)(asm.(*_assembler))
}

func (w *_listAssemblerRepr) Finish() error {
	return (*_listAssembler)(w).Finish()
}

tavit ohanian's avatar
tavit ohanian committed
785
func (w *_listAssemblerRepr) ValuePrototype(idx int64) ld.NodePrototype {
786 787 788 789 790
	panic("TODO: list ValuePrototype")
}

type _unionAssemblerRepr _unionAssembler

tavit ohanian's avatar
tavit ohanian committed
791
func (w *_unionAssemblerRepr) AssembleKey() ld.NodeAssembler {
792 793 794 795 796 797 798 799
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.UnionRepresentation_Keyed:
		return (*_unionAssembler)(w).AssembleKey()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
800
func (w *_unionAssemblerRepr) AssembleValue() ld.NodeAssembler {
801 802 803 804 805 806 807
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.UnionRepresentation_Keyed:
		key := w.curKey.val.String()
		revKey := inboundMappedType(w.schemaType, stg, key)
		w.curKey.val.SetString(revKey)

		valAsm := (*_unionAssembler)(w).AssembleValue()
808
		valAsm = assemblerRepr(valAsm)
809 810 811 812 813 814
		return valAsm
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
815
func (w *_unionAssemblerRepr) AssembleEntry(k string) (ld.NodeAssembler, error) {
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	if err := w.AssembleKey().AssignString(k); err != nil {
		return nil, err
	}
	am := w.AssembleValue()
	return am, nil
}

func (w *_unionAssemblerRepr) Finish() error {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.UnionRepresentation_Keyed:
		return (*_unionAssembler)(w).Finish()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

tavit ohanian's avatar
tavit ohanian committed
832
func (w *_unionAssemblerRepr) KeyPrototype() ld.NodePrototype {
833 834 835
	panic("TODO")
}

tavit ohanian's avatar
tavit ohanian committed
836
func (w *_unionAssemblerRepr) ValuePrototype(k string) ld.NodePrototype {
837 838 839 840 841
	panic("TODO: struct ValuePrototype")
}

type _structIteratorRepr _structIterator

tavit ohanian's avatar
tavit ohanian committed
842
func (w *_structIteratorRepr) Next() (key, value ld.Node, _ error) {
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
	_skipAbsent:
		key, value, err := (*_structIterator)(w).Next()
		if err != nil {
			return nil, nil, err
		}
		if value.IsAbsent() {
			goto _skipAbsent
		}
		keyStr, _ := key.AsString()
		mappedKey := outboundMappedKey(stg, keyStr)
		if mappedKey != keyStr {
			key = basicnode.NewString(mappedKey)
		}
		return key, reprNode(value), nil
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

func (w *_structIteratorRepr) Done() bool {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.StructRepresentation_Map:
		// TODO: the fact that repr map iterators skip absents should be
		// documented somewhere
		return w.nextIndex >= w.reprEnd
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

type _unionIteratorRepr _unionIterator

tavit ohanian's avatar
tavit ohanian committed
877
func (w *_unionIteratorRepr) Next() (key, value ld.Node, _ error) {
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.UnionRepresentation_Keyed:
		key, value, err := (*_unionIterator)(w).Next()
		if err != nil {
			return nil, nil, err
		}
		keyStr, _ := key.AsString()
		mappedKey := outboundMappedType(stg, keyStr)
		if mappedKey != keyStr {
			key = basicnode.NewString(mappedKey)
		}
		return key, reprNode(value), nil
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}

func (w *_unionIteratorRepr) Done() bool {
	switch stg := reprStrategy(w.schemaType).(type) {
	case schema.UnionRepresentation_Keyed:
		return (*_unionIterator)(w).Done()
	default:
		panic(fmt.Sprintf("TODO: %T", stg))
	}
}