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

Native Type Generation Support #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

code-monad
Copy link
Contributor

@code-monad code-monad commented Mar 30, 2023

What does this PR do

Brings the ability to generate native type conversion code for user defined types, gives a general interface that can easily integrated into legacy codes.

Example

Assume that we have a definition

struct StructA {
    f1: byte,
    f2: byte,
}

It will generates following codes:

type NativeStructA struct {
      // Note that *Byte is moleculec defined type, not the go intergrated `byte` type. Performs the same if you have a more complicated defined custom type
	F1 *Byte  `json:"f1"`
	F2 *Byte  `json:"f2"`
}

func UnpackStructA(v *StructA) *NativeStructA {
	native_val := &NativeStructA{}
	//s := v.AsBuilder()
	native_val.F1 = v.F1()
	native_val.F2 = v.F2()
	return native_val
}

Other mol internal types are mostly performs the same, users can use the test/schema/types.mol to generate and can explain the behaviour

How to use

Still the example of StructA, at the past we need to hand write our own native mapping structures and the Unpack func, now we can simply use the definition of NativeStuctA to stands the go type. And if the json fields name does not feats the meet, users can:

type MyPersonalStructA struct {
    	F1 *Byte  `json:"new_field_f1"`
	F2 *Byte  `json:"new_field_f2"`
}

and initialize it with:

// We have a NativeStructA
generated_a = &NativeStructA{}
//... other codes
personal_struct_a = MyPersonalStruct(generated_a)

@code-monad code-monad marked this pull request as draft March 31, 2023 00:00
@code-monad code-monad changed the title WIP: native type generation support Native Type Generation Support Mar 31, 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.

1 participant