Use a macro to modify nested structs?

Suppose I have a setup like this (I'm simplifying things a lot).

@Domain
struct SimpleDomain {
    struct Properties {
        var color: Color
        var area: Int
    }
    
    struct ObjectProperties {
        var object: Object
    }
    
    struct GroupProperties {
        var group: Group
    }
}

Now suppose I want the Domain macro to find the struct named Properties, extract its vars, and insert them into the other structs as computed properties. Is there any way to do this?

I'm experimenting with Domain as a member macro. I can find each of the nested structs, and find all of their vars. But I don't see any way I can modify ObjectProperties and GroupProperties. I don't think there's any way for a macro working at the level of the SimpleDomain struct to insert code inside the nested structs. And I can't extend ObjectProperties because you can't extend a struct inside another one (I know you can use extension SimpleDomain.ObjectProperties, but I doubt any macro can do that).

Alternatively, I could try applying a member macro directly to ObjectProperties and GroupProperties. But I don't know how that macro would have access to the vars defined inside Properties.

Any suggestions would be welcome. Thanks.

I’ve done something similar. What you need to do is use the outer macro to apply a macro attribute to the structs you want to modify, including all of the information from Properties you need as arguments to the macro.

1 Like

Thanks, this sounds like the right approach. I take it this means using MemberAttributeMacro? I haven't used that one, but I found an example here (I always struggle to find good official documentation on macros):

So it looks like this can take a member, check whether it's one of the ones I want to modify, and if it is, add a nested macro which will take the properties as arguments. And I can use the attachedTo argument to get my outer struct, so I can find the properties to apply to the appropriate inner structs?

It’s been awhile since I implemented it, but yes, that sounds correct.