Skip to content

mock: with-types-from-other-package

Back to gallery

Exhibit path: docs/examples/codegen/mock/with-types-from-other-package/

Methods whose signatures reference types from an imported package. Shows that the mock generator preserves the full qualified type name in the generated file and emits the correct import block.

Input (in.go)

go
//go:build glacier_codegen_fixture

// Package client defines the HTTP transport interface for the application.
package client

import (
	"context"
	"net/http"
)

// HTTPClient performs HTTP requests.
//
// +glacier:mock
type HTTPClient interface {
	// Do sends an HTTP request and returns an HTTP response.
	Do(ctx context.Context, req *http.Request) (*http.Response, error)
}

Output (out.go)

go
//go:build glacier_codegen_fixture

// Code generated by glaciergen. DO NOT EDIT.
// Source: in.go
package client

import (
	"context"
	"net/http"
)

// MockHTTPClient is a generated mock for HTTPClient.
type MockHTTPClient struct {
	// OnDo is called by Do. Set this in your test to control behavior.
	OnDo func(ctx context.Context, req *http.Request) (*http.Response, error)
}

// Do implements HTTPClient.
func (m *MockHTTPClient) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
	return m.OnDo(ctx, req)
}

What the generator did

  • Resolved the method signatures to their full qualified forms (e.g. http.Request, context.Context).
  • Emitted the correct import block in out.go with every referenced package.
  • The generated mock compiles in isolation: no manual import edits required after generation.

interface-only nested-interfaces glacier generate

Apache-2.0