Skip to content

Add codec factory registration and enhance type mapping functionality#102

Merged
freehere107 merged 3 commits intomasterfrom
optimize/interface
Feb 26, 2026
Merged

Add codec factory registration and enhance type mapping functionality#102
freehere107 merged 3 commits intomasterfrom
optimize/interface

Conversation

@freehere107
Copy link
Collaborator

No description provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the codec registry to register decoder factories (instead of shared instances), adds a codec cache for faster codec lookup/instantiation, and broadens encode support for more Go slice types while adding targeted tests/benchmarks.

Changes:

  • Replace the reflection-based registry with CodecFactory registration plus a concurrency-safe (module,spec,typeString) codec cache.
  • Expand encoding to accept more concrete slice types (e.g., []uint32, []int) via helper conversion utilities.
  • Add new unit tests/benchmarks for type mapping access, vec encoding, U256 encoding, and cache concurrency.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
utiles/tools.go Removes unused reflect-based nil helper and cleans imports.
types/v13.go Registers OriginCaller via factory to avoid shared decoder state.
types/types_test.go Adds assertions/tests for vec encoding slice types and U256 byte-slice encoding.
types/types.go Updates multiple Encode signatures to interface{} and improves some decode/encode internals.
types/type_mapping_test.go Adds test verifying Consensus exposes a TypeMapping via TypeMappingGetter.
types/registry_types.go Adds generated list of base codec type names (input to registry generator).
types/registry_gen.go Adds generated baseCodecFactories map (type name → factory).
types/registry.go Implements factory-based registry, codec cache, and GetCodec API returning (Decoder, subType, error).
types/encode_helpers.go Adds helpers to normalize many concrete slice types into []interface{} for encoders.
types/customType_test.go Adds concurrency test covering codec cache + encode/decode under goroutines.
types/customType.go Migrates custom type registration to factory functions; resets codec cache on registration.
types/benchmark_test.go Adds benchmarks for decode/encode hot paths.
types/basic_types_test.go Adds tests for Bytes/HexBytes/Option/Null/hash types encode/decode.
types/base.go Introduces Decoder/Encoder interfaces, adds fast-path decoding for common primitives, updates encode path to use GetCodec.
types/Vectors.go Refactors Vec.Encode to accept more slice types without reflection.
types/Uint.go Refactors uint decode/encode (less allocation) and extends U256 encoding inputs.
types/Struct.go Updates struct encoding to accept interface{} and type assert to map.
types/Results.go Updates result encoding to accept interface{} and type assert to map.
types/Option.go Replaces reflect-based nil detection with explicit checks for common typed-nil cases.
types/FixedArray.go Refactors fixed array encoding to support more slice types without reflection.
types/Bytes.go Updates Bytes/HexBytes/String encoders to accept interface{} + type assertion.
types/Bool.go Updates Bool encoder to accept interface{} + type assertion.
tools/gen_registry/main.go Adds generator that produces registry_gen.go from registry_types.go.
.github/workflows/ci.yml Updates Go GitHub Actions to newer major versions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

types/Option.go:56

  • Option.Encode no longer treats typed-nil values (e.g. (*T)(nil), []uint32(nil), map[string]string(nil), etc.) as empty options. Since value == nil is false when an interface holds a typed-nil, these will currently fall through and be encoded as Some(...) (or panic for bool), which is a behavior change from the prior utiles.IsNil implementation.

Consider restoring a general typed-nil check (e.g. a small helper using reflect.ValueOf(value).Kind() + IsNil() for ptr/map/slice/chan/func/interface) or otherwise broadening the nil detection so all typed-nil values encode as 00.

func (o *Option) Encode(value interface{}) string {
	if v, ok := value.(string); ok && v == "" {
		return "00"
	}
	if value == nil {
		return "00"
	}
	switch v := value.(type) {
	case []byte:
		if v == nil {
			return "00"
		}
	case []interface{}:
		if v == nil {
			return "00"
		}
	case map[string]interface{}:
		if v == nil {
			return "00"
		}
	case error:
		if v == nil {
			return "00"
		}
	}
	if o.SubType == "bool" {
		if value.(bool) {
			return "01"
		}
		return "02"
	}
	return "01" + EncodeWithOpt(o.SubType, value, &ScaleDecoderOption{Spec: o.Spec, Metadata: o.Metadata})
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@freehere107 freehere107 merged commit 0ca14d2 into master Feb 26, 2026
5 checks passed
@freehere107 freehere107 deleted the optimize/interface branch February 26, 2026 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants