Пример #1
0
func ExampleTypeRegistry_New() {
	registry := typeregistry.New()
	name := registry.Add(simpleThing{})
	thing := registry.New(name)
	fmt.Printf("%#v", thing)
	// Output: typeregistry_test.simpleThing{Name:""}
}
func ExampleTypeRegistry_Unmarshal() {
	registry := typeregistry.New()
	registry.Add(&userThing{})

	// The service world.
	var (
		ryan   = &User{"1", "Ryan"}
		svc    = &UserService{[]*User{ryan}}
		sample = &userThing{user: ryan}
	)

	// Pass between marshal and unmarshal.
	var (
		name string
		data []byte
	)

	// Marshal it.
	func() {
		var err error
		name, data, err = registry.Marshal(sample)
		if err != nil {
			panic(err)
		}
		fmt.Printf("Sample marshaled name:%s data:%s\n", name, data)
	}()

	// Unmarshal it with custom setup.
	func() {
		si, err := registry.Unmarshal(name, data, func(o interface{}) {
			if s, ok := o.(*userThing); ok {
				s.svc = svc
			}
		})
		if err != nil {
			panic(err)
		}
		sample := si.(*userThing)
		fmt.Printf("Sample unmarshaled with ID:%s, Name:%s\n", sample.UserID, sample.user.Name)
	}()
	// Output:
	// Sample marshaled name:*typeregistry_test.userThing data:{"UserID":"1"}
	// Sample unmarshaled with ID:1, Name:Ryan
}