// NewUserTypeDefinition creates a user type definition but does not
// execute the DSL.
162436
func NewUserTypeDefinition(name string, dsl func()) *UserTypeDefinition {
	return &UserTypeDefinition{
		TypeName:            name,
		AttributeDefinition: &AttributeDefinition{DSLFunc: dsl},
	}
}
364980
func actionDefinition() (*design.ActionDefinition, bool) {
	a, ok := dslengine.CurrentDefinition().(*design.ActionDefinition)
	if !ok {
		dslengine.IncompatibleDSL()
	}
	return a, ok
}
416749
func NewLoginFailType(Description string) *LoginFailType {
	s := new(LoginFailType)
	s.Description = Description
	return s
}
1200685
func NewAppLinkUserType(Description string) *AppLinkUserType {
	s := new(AppLinkUserType)
	s.Description = Description
	return s
}
644484
func NewHolderType(s string) (holderType, error) {
	var e holderType
	err := e.FromString(s)
	if err != nil {
		return 0, err
	}
	return e, nil
}
44781
func NewFileEditType(Description string) *FileEditType {
	s := new(FileEditType)
	s.Description = Description
	return s
}
291706
func NewLoginSuccessType(Description string) *LoginSuccessType {
	s := new(LoginSuccessType)
	s.Description = Description
	return s
}
645802
func NewDirectoryRestrictionsAddMembersType(Description string) *DirectoryRestrictionsAddMembersType {
	s := new(DirectoryRestrictionsAddMembersType)
	s.Description = Description
	return s
}
412755
func NewUserVersion(uid keybase1.UID, eldestSeqno keybase1.Seqno) keybase1.UserVersion {
	return keybase1.NewUserVersion(uid, eldestSeqno)
}
946600
func NewMemberPermanentlyDeleteAccountContentsType(Description string) *MemberPermanentlyDeleteAccountContentsType {
	s := new(MemberPermanentlyDeleteAccountContentsType)
	s.Description = Description
	return s
}
685940
func NewPaperFolderDeletedType(Description string) *PaperFolderDeletedType {
	s := new(PaperFolderDeletedType)
	s.Description = Description
	return s
}
538424
func typeDefinition() (*design.UserTypeDefinition, bool) {
	m, ok := dslengine.CurrentDefinition().(*design.UserTypeDefinition)
	if !ok {
		dslengine.IncompatibleDSL()
	}
	return m, ok
}
574215
func NewDomainInvitesEmailExistingUsersDetails(DomainName string, NumRecipients uint64) *DomainInvitesEmailExistingUsersDetails {
	s := new(DomainInvitesEmailExistingUsersDetails)
	s.DomainName = DomainName
	s.NumRecipients = NumRecipients
	return s
}
178900
func NewUserTypesWriter(filename string) (*UserTypesWriter, error) {
	file, err := codegen.SourceFileFor(filename)
	if err != nil {
		return nil, err
	}
	return &UserTypesWriter{SourceFile: file}, nil
}
1223154
func corsDefinition() (*design.CORSDefinition, bool) {
	cors, ok := dslengine.CurrentDefinition().(*design.CORSDefinition)
	if !ok {
		dslengine.IncompatibleDSL()
	}
	return cors, ok
}
329939
func encodingDefinition() (*design.EncodingDefinition, bool) {
	e, ok := dslengine.CurrentDefinition().(*design.EncodingDefinition)
	if !ok {
		dslengine.IncompatibleDSL()
	}
	return e, ok
}
436081
func (g *Generator) DomainDefinition(d proto.Domain) {
	g.hasContent = true

	comment := fmt.Sprintf("domainClient is a client for the %s domain. ", d.Name())
	g.Printf(`
// %[1]s%[3]s
type domainClient struct{ conn *rpcc.Conn }

// NewClient returns a client for the %[2]s domain with the connection set to conn.
func NewClient(conn *rpcc.Conn) *domainClient {
	return &domainClient{conn: conn}
}
`, comment, d.Name(), d.Desc(0, len(comment)))

	for _, c := range d.Commands {
		if c.Redirect != "" {
			continue
		}
		request := ""
		invokeReply := "nil"
		if len(c.Parameters) > 0 {
			request = ", args *" + c.ArgsName(d)
		}
		reply := "(err error)"
		if len(c.Returns) > 0 {
			reply = fmt.Sprintf("(reply *%s, err error)", c.ReplyName(d))
		}
		comment := fmt.Sprintf("%[1]s invokes the %[2]s method. ", c.Name(), d.Name())
		g.Printf(`
// %[2]s%[5]s
func (d *domainClient) %[1]s(ctx context.Context%[3]s) %[4]s {`, c.Name(), comment, request, reply, c.Desc(true, 0, len(comment)))
		if len(c.Returns) > 0 {
			g.Printf(`
	reply = new(%s)`, c.ReplyName(d))
			invokeReply = "reply"
		}
		if len(c.Parameters) > 0 {
			g.Printf(`
	if args != nil {
		err = rpcc.Invoke(ctx, %[1]q, args, %[2]s, d.conn)
	} else {
		err = rpcc.Invoke(ctx, %[1]q, nil, %[2]s, d.conn)
	}
	if err != nil {
		err = &internal.OpError{Domain: %[3]q, Op: %[4]q, Err: err}
	}
	return
}
`, d.Domain+"."+c.NameName, invokeReply, d.Name(), c.Name())
		} else {
			g.Printf(`
	err = rpcc.Invoke(ctx, %q, nil, %s, d.conn)
	if err != nil {
		err = &internal.OpError{Domain: %q, Op: %q, Err: err}
	}
	return
}
`, d.Domain+"."+c.NameName, invokeReply, d.Name(), c.Name())
		}

		// Generate method tests.
		g.TestPrintf(`
func Test%[1]s_%[2]s(t *testing.T) {
	conn, codec, cleanup := newTestConn(t)
	defer cleanup()

	dom := New%[1]s(conn)
	var err error
`, d.Name(), c.Name())
		assign := "err"
		if len(c.Returns) > 0 {
			assign = "_, err"
		}
		if len(c.Parameters) > 0 {
			g.TestPrintf(`
	// Test nil args.
	%[1]s = dom.%[2]s(nil, nil)
	if err != nil {
		t.Error(err)
	}
	// Test args.
	%[1]s = dom.%[2]s(nil, &%[3]s{})`, assign, c.Name(), c.ArgsName(d))
		} else {
			g.TestPrintf(`
	%[1]s = dom.%[2]s(nil)`, assign, c.Name())
		}
		g.TestPrintf(`
	if err != nil {
		t.Error(err)
	}

	// Test error.
	codec.respErr = errors.New("bad request")`)
		if len(c.Parameters) > 0 {
			g.TestPrintf(`
	%[1]s = dom.%[2]s(nil, &%[3]s{})`, assign, c.Name(), c.ArgsName(d))
		} else {
			g.TestPrintf(`
	%[1]s = dom.%[2]s(nil)`, assign, c.Name())
		}
		g.TestPrintf(`
	if err == nil || err.(*internal.OpError).Err.(*rpcc.ResponseError).Message != codec.respErr.Error() {
		t.Errorf("unexpected error; got: %%v, want bad request", err)
	}`)
		g.TestPrintf(`
}
`)

	}
	for _, e := range d.Events {
		eventClient := fmt.Sprintf("%sClient", e.EventName(d))
		eventClientImpl := strings.ToLower(string(eventClient[0])) + eventClient[1:]

		// Implement event on domain.
		g.Printf(`
func (d *domainClient) %s(ctx context.Context) (%s, error) {
	s, err := rpcc.NewStream(ctx, %q, d.conn)
	if err != nil {
		return nil, err
	}
	return &%s{Stream: s}, nil
}
`, e.Name(), eventClient, d.Domain+"."+e.NameName, eventClientImpl)

		g.Printf(`
type %[4]s struct { rpcc.Stream }

// GetStream returns the original Stream for use with cdp.Sync.
func (c *%[4]s) GetStream() rpcc.Stream { return c.Stream }

func (c *%[4]s) Recv() (*%[3]s, error) {
	event := new(%[3]s)
	if err := c.RecvMsg(event); err != nil {
		return nil, &internal.OpError{Domain: %[5]q, Op: "%[6]s Recv", Err: err}
	}
	return event, nil
}
`, eventClient, "", e.ReplyName(d), eventClientImpl, d.Name(), e.Name())

		// Generate event tests.
		g.TestPrintf(`
func Test%[1]s_%[2]s(t *testing.T) {
	conn, codec, cleanup := newTestConn(t)
	defer cleanup()

	dom := New%[1]s(conn)

	stream, err := dom.%[2]s(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer stream.Close()

	codec.event = %[3]s.String()
	codec.conn <- nil
	_, err = stream.Recv()
	if err != nil {
		t.Error(err)
	}

	codec.eventArgs = []byte("invalid json")
	codec.conn <- nil
	_, err = stream.Recv()
	if err, ok := err.(*internal.OpError); !ok {
		t.Errorf("Recv() got %%v, want internal.OpError", err)
	}

	conn.Close()
	stream, err = dom.%[2]s(nil)
	if err == nil {
		t.Errorf("Open stream: got nil, want error")
	}
`, d.Name(), e.Name(), e.EventName(d))
		g.TestPrintf(`
}
`)
	}
}