Skip to content

bug: Cannot specifically skip fields using WithSkipFields on a sub struct's field being unwrapped using WithUnwrapStructFields #19124

@jsifuentes

Description

@jsifuentes

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I have a struct that looks like this:

type ComputerInventory struct {
	ID                    string                                              `json:"id,omitempty"`
	Udid                  string                                              `json:"udid,omitempty"`
	... (omitted)
	General               *ComputerInventoryDataSubsetGeneral                 `json:"general,omitempty"`
	ExtensionAttributes   *[]ComputerInventoryDataSubsetExtensionAttributes   `json:"extensionAttributes,omitempty"`
}

type ComputerInventoryDataSubsetGeneral struct {
	Name              string `json:"name,omitempty"`
	...
	ExtensionAttributes                  []struct {
		DefinitionID string   `json:"definitionId,omitempty"`
		Name         string   `json:"name,omitempty"`
		... (omitted)
	} `json:"extensionAttributes,omitempty"`
}

Note that ComputerInventory and ComputerInventoryDataSubsetGeneral contain a ExtensionAttribute field.

I have this table:

func ComputerInventoryTable() *schema.Table {
	return &schema.Table{
		Name:        "jamf_computer_inventory",
		Description: "Jamf Computers",
		Transform: transformers.TransformWithStruct(
			&jamf.ComputerInventory{},
			transformers.WithPrimaryKeys("ID"),
			transformers.WithUnwrapStructFields("General"),
			// transformers.WithSkipFields(),
		),
		Resolver: fetchComputers,
	}
}

So during column creation, General is unwrapped, leading to an extension_attributes and a general_extension_attributes field in the database.

Goal: I want to skip the root ComputerInventory ExtensionAttributes field, but not skip the General one.
What I do: Add ExtensionAttributes to the WithSkipFields
What actually happens: Both ExtensionAttributes fields are removed -- the root and the child one.

Expected Behavior

ExtensionAttributes on the root struct is removed while the General.ExtensionAttributes field remains.

CloudQuery (redacted) config

unneccessary

Steps To Reproduce

  1. Have a struct like the following:
type Example struct {
  ExtensionAttributes string
  Child struct {
    ExtensionAttributes string
  }
}
  1. Construct a table with this struct. Unwrap the Child field. Skip the ExtensionAttributes field.
func AbcTable() *schema.Table {
	return &schema.Table{
		Name:        "abc",
		Description: "abc",
		Transform: transformers.TransformWithStruct(
			&Example{},
			transformers.WithUnwrapStructFields("Child"),
			transformers.WithSkipFields("ExtensionAttributes"),
		),
		Resolver: fetchAbc,
	}
}

Expectation: Root ExtensionAttributes field is skipped while the Child field remains.

CloudQuery (redacted) logs

CloudQuery version

6.5.0

Additional Context

In the plugin-sdk when it starts unwrapping fields, it uses t.ignoreField(sf) to determine whether it should skip a field.

https://github.com/cloudquery/plugin-sdk/blob/b185e11bad937fbbeb9178f88f0ede749088efc7/transformers/struct.go#L58-L74

But sf.Name is simply just ExtensionAttributes for both the root field and the internal General struct being unwrapped. So there is no way to distinguish between the two. sf.Name perhaps should be prefixed with the name of the parent field. That way, you can do WithSkipFields("ExtensionAttributes") vs. WithSkipFields("General.ExtensionAttributes") or something.

Pull request (optional)

  • I can submit a pull request

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions