Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add custom module type support #20

Merged
merged 2 commits into from
May 23, 2023

Conversation

kuzznya
Copy link
Contributor

@kuzznya kuzznya commented May 13, 2023

This is a suggestion to add support for custom module type parsing by allowing the user of a library to implement it.

Example usage:

const myType = "mytypeid"

func parse() {
	decoder := core.NewDecoder(r).WithSpecialType(ftsIndexType, parseMyType)
}

func parseMyType(h core.ModuleTypeHandler, encVersion int) (interface{}, error) {
	opcode, err := h.ReadOpcode()
	if err != nil {
		return nil, err
	}
	if opcode != core.OpcodeString {
		return nil, errors.New("unsupported module type value code")
	}

	data, err := h.ReadString()
	if err != nil {
		return nil, err
	}

	val := model.MyModel{}
	err = json.Unmarshal(data, &val)
	if err != nil {
		return nil, err
	}

	opcode, err = h.ReadOpcode()
	if err != nil || opcode != core.OpcodeEOF {
		return nil, errors.New("expected module type value EOF")
	}

	return &val, nil
}

This implementation was used in my fork for my project, but it would be nice to have it supported in your library.

I'm open to any comments about the design of the solution and ready to update the PR in my free time if anything required.

@kuzznya
Copy link
Contributor Author

kuzznya commented May 13, 2023

I can write the tests once the approach itself would be approved

@HDT3213
Copy link
Owner

HDT3213 commented May 14, 2023

Actually, I think it is a good idea. Please add some tests so I can merge it into master

@kuzznya
Copy link
Contributor Author

kuzznya commented May 19, 2023

Hi @HDT3213, I've created the test similar to existing ones. Failed to reach the same coverage results, so if required, I can create some more, but at least main case is covered

@HDT3213 HDT3213 merged commit d8465d2 into HDT3213:master May 23, 2023
@HDT3213 HDT3213 mentioned this pull request Jul 6, 2023
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