<commit_before>package lib

import (
	"fmt"

	"veyron2/ipc"
	"veyron2/vdl"
	"veyron2/wiretype"
)

const (
	// agreed-upon name of the signature method that's available on all services
	signatureMethodName = "Signature"
)

var (
	anydataType = wiretype.NamedPrimitiveType{
		Name: "veyron2/vdl.AnyData",
		Type: wiretype.TypeIDInterface,
	}
	errType = wiretype.NamedPrimitiveType{
		Name: "error",
		Type: wiretype.TypeIDInterface,
	}
	anydataTypeID = wiretype.TypeIDFirst
	errTypeID     = wiretype.TypeIDFirst
)

// JSONServiceSignature represents the information about a service signature that is used by JSON.
type JSONServiceSignature map[string]JSONMethodSignature

// JSONMethodSignature represents the information about a method signature that is used by JSON.
type JSONMethodSignature struct {
	InArgs      []string // InArgs is a list of argument names.
	NumOutArgs  int
	IsStreaming bool
}

// NewJSONServiceSignature converts an ipc service signature to the format used by JSON.
func NewJSONServiceSignature(sig ipc.ServiceSignature) JSONServiceSignature {
	jsig := JSONServiceSignature{}

	for name, methSig := range sig.Methods {
		jmethSig := JSONMethodSignature{
			InArgs:      make([]string, len(methSig.InArgs)),
			NumOutArgs:  len(methSig.OutArgs),
			IsStreaming: methSig.InStream != wiretype.TypeIDInvalid || methSig.OutStream != wiretype.TypeIDInvalid,
		}

		for i, inarg := range methSig.InArgs {
			jmethSig.InArgs[i] = inarg.Name
		}

		jsig[lowercaseFirstCharacter(name)] = jmethSig
	}

	return jsig
}

// ServiceSignature converts a JSONServiceSignature to an ipc service signature.
func (jss JSONServiceSignature) ServiceSignature() (ipc.ServiceSignature, error) {
	ss := ipc.ServiceSignature{
		Methods: make(map[string]ipc.MethodSignature),
	}

	for name, sig := range jss {
		ms := ipc.MethodSignature{}

		ms.InArgs = make([]ipc.MethodArgument, len(sig.InArgs))
		for i, argName := range sig.InArgs {
			ms.InArgs[i] = ipc.MethodArgument{
				Name: argName,
				Type: anydataTypeID,
			}
		}
		if sig.NumOutArgs != 2 {
			return ipc.ServiceSignature{}, fmt.Errorf("cannot create service signature for service without exactly one return value and one error")
		}
		ms.OutArgs = make([]ipc.MethodArgument, 2)
		ms.OutArgs[0] = ipc.MethodArgument{
			Type: anydataTypeID,
		}
		ms.OutArgs[1] = ipc.MethodArgument{
			Name: "err",<commit_msg>veyron/javascript/api: Added the ability to have void functions.<commit_after>package lib

import (
	"veyron2/ipc"
	"veyron2/vdl"
	"veyron2/wiretype"
)

const (
	// agreed-upon name of the signature method that's available on all services
	signatureMethodName = "Signature"
)

var (
	anydataType = wiretype.NamedPrimitiveType{
		Name: "veyron2/vdl.AnyData",
		Type: wiretype.TypeIDInterface,
	}
	errType = wiretype.NamedPrimitiveType{
		Name: "error",
		Type: wiretype.TypeIDInterface,
	}
	anydataTypeID = wiretype.TypeIDFirst
	errTypeID     = wiretype.TypeIDFirst
)

// JSONServiceSignature represents the information about a service signature that is used by JSON.
type JSONServiceSignature map[string]JSONMethodSignature

// JSONMethodSignature represents the information about a method signature that is used by JSON.
type JSONMethodSignature struct {
	InArgs      []string // InArgs is a list of argument names.
	NumOutArgs  int
	IsStreaming bool
}

// NewJSONServiceSignature converts an ipc service signature to the format used by JSON.
func NewJSONServiceSignature(sig ipc.ServiceSignature) JSONServiceSignature {
	jsig := JSONServiceSignature{}

	for name, methSig := range sig.Methods {
		jmethSig := JSONMethodSignature{
			InArgs:      make([]string, len(methSig.InArgs)),
			NumOutArgs:  len(methSig.OutArgs),
			IsStreaming: methSig.InStream != wiretype.TypeIDInvalid || methSig.OutStream != wiretype.TypeIDInvalid,
		}

		for i, inarg := range methSig.InArgs {
			jmethSig.InArgs[i] = inarg.Name
		}

		jsig[lowercaseFirstCharacter(name)] = jmethSig
	}

	return jsig
}

// ServiceSignature converts a JSONServiceSignature to an ipc service signature.
func (jss JSONServiceSignature) ServiceSignature() (ipc.ServiceSignature, error) {
	ss := ipc.ServiceSignature{
		Methods: make(map[string]ipc.MethodSignature),
	}

	for name, sig := range jss {
		ms := ipc.MethodSignature{}

		ms.InArgs = make([]ipc.MethodArgument, len(sig.InArgs))
		for i, argName := range sig.InArgs {
			ms.InArgs[i] = ipc.MethodArgument{
				Name: argName,
				Type: anydataTypeID,
			}
		}

		ms.OutArgs = make([]ipc.MethodArgument, sig.NumOutArgs)
		for i := 0; i < sig.NumOutArgs-1; i++ {
			ms.OutArgs[i] = ipc.MethodArgument{
				Type: anydataTypeID,
			}
		}
		ms.OutArgs[sig.NumOutArgs-1] = ipc.MethodArgument{
			Name: "err",
{'fraction_non_alphanumeric': 0.10142765821436181, 'fraction_numerical': 0.0031962497336458554, 'mean_word_length': 4.3401592718998865, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064761', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1503, 'n_words': 390}