doutucui0133
2014-04-05 23:03
浏览 47
已采纳

如何对使用联合的C结构建模Go绑定?

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?

图片转代码服务由CSDN问答提供 功能建议

我目前正在写使用包装器进行 libfreefare 。 libfreefare的API包含以下功能:

  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; 
结构{
 int32_t lower_limit; 
 int32_t upper_limit; 
 int32_t limited_credit_value; 
 uint8_t limited_credit_enabled; 
} value_file; 
结构{
 uint32_t record_  uint32_t max_number_of_records; 
 uint32_t current_number_of_records; 
} linear_record_file; 
}设置; 
}; 
 
int mifare_desfire_get_file_settings(MifareTag标记,uint8_t file_no,struct mifare_desfire_file _ >> 包装此类功能的理想解决方案是什么? 如果 struct mifare_desfire_file_settings 不包含任何联合,则我的包装程序可能看起来像这样: 
 
 
  type DESFireFileSettings struct {
 //所有字段 导出的,没有方法
} 
 
func(t DESFireTag)FileSettings(文件无字节)(DESFireFileSettings,错误)
   
 
 

我应该如何进行?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongpao9165 2014-04-06 00:45
    最佳回答

    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
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题