简体   繁体   中英

How to model Go bindings to C structs that use unions?

I'm currently writing a Go wrapper for the libfreefare . The API of the libfreefare contains the following function:

struct mifare_desfire_file_settings {
    uint8_t file_type;
    uint8_t communication_settings;
    uint16_t access_rights;
    union {
    struct {
        uint32_t file_size;
    } standard_file;
    struct {
        int32_t lower_limit;
        int32_t upper_limit;
        int32_t limited_credit_value;
        uint8_t limited_credit_enabled;
    } value_file;
    struct {
        uint32_t record_size;
        uint32_t max_number_of_records;
        uint32_t current_number_of_records;
    } linear_record_file;
    } settings;
};

int      mifare_desfire_get_file_settings (MifareTag tag, uint8_t file_no, struct mifare_desfire_file_settings *settings);

What is the ideomatic solution for wrapping such a function? If the struct mifare_desfire_file_settings wouldn't contain any unions, my wrapper would probably look like this:

type DESFireFileSettings struct {
    // all fields exported, no methods
}

func (t DESFireTag) FileSettings(fileNo byte) (DESFireFileSettings, error)

How should I proceed?

You need to consider how you would update fields in the union. Obviously, you can't let users do that without validation. They could do inconsistent updates. Consider doing something like this:

package mifare

const (
    MDFTStandarDataFile            = 0x00
    MDFTBackupDataFile             = 0x01
    MDFTValueFileWithBackup        = 0x02
    MDFTLinearRecordFileWithBackup = 0x03
    MDFTCyclicRecordFileWithBackup = 0x04
)

type StandardFile struct {
    FileSize uint32
}

type ValueFile struct {
    LowerLimit           int32
    UpperLimit           int32
    LimitedCreditValue   int32
    LimitedCreditEnabled uint8
}

type LinearRecordFile struct {
    Record_size            uint32
    MaxNumberOfRecords     uint32
    CurrentNumberOfRecords uint32
}

type DESFireFileSettings struct {
    FileType              uint8
    CommunicationSettings uint8
    AccessRights          uint16
    settings              struct {
        StandardFile
        ValueFile
        LinearRecordFile
    }
}

func (fs *DESFireFileSettings) StandardFile() (StandardFile, error) {
    // if not valid for FileType, return error
    return fs.settings.StandardFile, nil
}

func (fs *DESFireFileSettings) SetStandardFile(standardFile StandardFile) error {
    // if not valid for FileType, return error
    fs.settings.StandardFile = standardFile
    return nil
}

func (fs *DESFireFileSettings) ValueFile() (ValueFile, error) {
    // if not valid for FileType, return error
    return fs.settings.ValueFile, nil
}

func (fs *DESFireFileSettings) SetValueFile(valueFile ValueFile) error {
    // if not valid for FileType, return error
    fs.settings.ValueFile = valueFile
    return nil
}

func (fs *DESFireFileSettings) LinearRecordFile() (LinearRecordFile, error) {
    // if not valid for FileType, return error
    return fs.settings.LinearRecordFile, nil
}

func (fs *DESFireFileSettings) SetLinearRecordFile(linearRecordFile LinearRecordFile) error {
    // if not valid for FileType, return error
    fs.settings.LinearRecordFile = linearRecordFile
    return nil
}

I partially agree with peterSO, but I'd take a different approach to the DESFireFileSettings struct since this is a wrapper and not a port. (If this were a port I'd use interfaces instead).

Consider, similar to what he wrote:

type StandardFile struct {
    FileSize uint32

    _ [9]byte // for padding to size of ValueFile
}

type ValueFile struct {
    LowerLimit           int32
    UpperLimit           int32
    LimitedCreditValue   int32
    LimitedCreditEnabled uint8
}

type LinearRecordFile struct {
    Record_size            uint32
    MaxNumberOfRecords     uint32
    CurrentNumberOfRecords uint32

    _ [1]byte // for padding to size of ValueFile
}

However, for DESFireFileSettings

type DESFireFileSettings struct {
    fs C.mifare_desfire_get_file_settings
}

Note that in Go unions are recorded as [n]byte where n is the biggest size of the union. So in this case n is 13 (three 4-byte int32 and one 1-byte uint8). Using unsafe:

func (dfs *DESFireFileSettings) SetStandardFile(standardFile StandardFile) error {
     // If not valid, return an error
     dfs.fs.settings = *((*[13]byte)(unsafe.Pointer(&standardFile)))
     return nil
}

If you want to avoid unsafe, you can make it more explicit with a C function

void setStandardFile(*mifare_desfire_file_settings fs, uint32_t size) {
    fs.settings.standardfile.file_size = size
}

And write a passthrough function in Go:

func (dfs *DESFireFileSettings) SetStandardFile(standardFile StandardFile) error {
      // If not valid, return an error
      C.setStandardFile(&dfs.fs, C.uint32_t(standardFile.FileSize))
 }

I, personally, prefer directly setting it with unsafe in this context, but both ways are valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM