func()

in internal/typeregistry/registration.go [120:153]


func (t *TypeRegistry) RegisterStruct(fqn api.FQN, strct reflect.Type) error {
	if strct.Kind() != reflect.Struct {
		return fmt.Errorf("the provided struct is not a struct: %v", strct)
	}

	if existing, exists := t.fqnToType[fqn]; exists && existing.Type != strct {
		return fmt.Errorf("another type was already registered with %v: %v", fqn, existing)
	}

	if existing, exists := t.structInfo[strct]; exists && existing.FQN != fqn {
		return fmt.Errorf("attempting to register type %v as %v, but it was already registered as: %v", strct, fqn, existing.FQN)
	}

	numField := strct.NumField()
	fields := make([]reflect.StructField, 0, numField)
	for i := 0; i < numField; i++ {
		field := strct.Field(i)
		if field.Anonymous {
			return fmt.Errorf("unexpected anonymous field %v in struct %v (%v)", field, fqn, strct)
		}
		if field.PkgPath != "" {
			return fmt.Errorf("unexpected un-exported field %v in struct %v (%v)", field, fqn, strct)
		}
		if field.Tag.Get("json") == "" {
			return fmt.Errorf("missing json tag on struct field %v of %v (%v)", field, fqn, strct)
		}
		fields = append(fields, field)
	}

	t.fqnToType[fqn] = registeredType{strct, structType}
	t.structInfo[strct] = registeredStruct{FQN: fqn, Fields: fields}

	return nil
}