Пример #1
0
// Return the QueryResult to simulate factory get behavior
func (s *MockFactoryService) HandleGet(ctx context.Context, op *operation.Operation) {
	type factoryQueryResult struct {
		common.ServiceDocumentQueryResult
		Documents map[string]interface{} `json:"documents,omitempty"`
	}
	result := factoryQueryResult{}
	result.Documents = s.ServiceMap
	op.SetBody(result).Complete()
}
Пример #2
0
// handlePost calls out to the factory service implementation's POST handler,
// if it exists, and waits for completion. If this runs and completes without
// error, the returned body is passed to the start operation for the service
// created by this factory.
func (f *FactoryServiceContext) handlePost(ctx context.Context, op *operation.Operation) {
	var err error
	var sd *common.ServiceDocument

	if h, ok := f.h.(PostHandler); ok {
		// Run the factory service's POST handler and wait for completion.
		err = op.CreateChild(ctx).Go(ctx, h.HandlePost).Wait()
		if err != nil {
			op.Fail(err)
			return
		}
	}

	doc := f.h.CreateDocument()
	err = op.DecodeBody(doc)
	if err != nil && err != io.EOF {
		op.Fail(err)
		return
	}

	sd = doc.GetServiceDocument()
	sd.SelfLink = path.Join(f.SelfLink(), uuid.New())
	op.SetBody(doc)

	buf, err := op.EncodeBodyAsBuffer()
	if err != nil {
		op.Fail(err)
		return
	}

	// Start child service at service document's selflink
	startOp := op.NewPost(ctx, uri.Extend(uri.Local(), sd.SelfLink), buf)
	f.Host().StartService(startOp, f.h.CreateService())
	err = startOp.Wait()
	if err != nil {
		op.Fail(err)
		return
	}

	op.Complete()
}
Пример #3
0
func (s *MockFactoryService) HandlePost(ctx context.Context, op *operation.Operation) {
	state := s.createStatef()
	op.DecodeBody(&state)

	val := reflect.ValueOf(state).Elem()
	f := val.FieldByName("SelfLink")
	var selfLink string
	if f.Kind() == reflect.String && f.Len() > 0 {
		selfLink = f.String()
	}

	if len(selfLink) == 0 {
		selfLink = s.factoryURI + "/" + uuid.New()
		if f.Len() == 0 {
			f.SetString(selfLink)
		}
	}
	_ = s.th.StartMockWithSelfLink(state, selfLink)
	s.ServiceMap[selfLink] = state
	op.SetBody(state).Complete()
}
Пример #4
0
func (s *ServiceContext) HandleGet(ctx context.Context, op *operation.Operation) {
	op.SetBody(s.h.GetState()).Complete()
}