在Go中转换结构类型

时间:2016-08-26 15:40:46

标签: go reflection struct generated-code

所以我有一个问题,我一直在试图找出一段时间,希望有人有一些见解。基本上,我在Go中有两个相似但不同的结构,我想在它们之间进行转换。

如下所示:

B.Name == NameStruct{
              Firstname: strings.Split(A.Name, " ")[0],
              Lastname:  strings.Split(A.Name, " ")[1],
          }

在此示例中,

--Create a table for test data (modeled after the Northwind Employees table).
CREATE TABLE EMPLOYEES
(
    EMPLOYEE_ID INT NOT NULL,
    LASTNAME VARCHAR(20) NOT NULL,
    FIRSTNAME VARCHAR(10) NOT NULL,
    TITLE VARCHAR(30),
    TITLE_OF_COURTESY VARCHAR(25),
    BIRTHDATE DATE,
    HIREDATE DATE,
    ADDRESS VARCHAR(60),
    CITY VARCHAR(15),
    REGION VARCHAR(15),
    POSTAL_CODE VARCHAR(10),
    COUNTRY VARCHAR(15),
    HOME_PHONE VARCHAR(24),
    EXTENSION VARCHAR(4),
    PHOTO VARCHAR(255),
    NOTES VARCHAR(2000),
    REPORTS_TO INT,
    CONSTRAINT PK_EMPLOYEES PRIMARY KEY (EMPLOYEE_ID)
);
GO
--end Create a table for test data (modeled after the Northwind Employees table).

--Insert some data
DECLARE @INSERT_DATA_ROW_COUNTER INT
SET @INSERT_DATA_ROW_COUNTER=0
SET NOCOUNT ON
WHILE @INSERT_DATA_ROW_COUNTER<1500
BEGIN
    INSERT INTO EMPLOYEES 
    (EMPLOYEE_ID, 
    LASTNAME,
    FIRSTNAME,
    TITLE,
    TITLE_OF_COURTESY,
    BIRTHDATE,
    HIREDATE,
    ADDRESS,
    CITY,
    REGION,
    POSTAL_CODE,
    COUNTRY,
    HOME_PHONE,
    EXTENSION,
    PHOTO,
    NOTES,
    REPORTS_TO)
    VALUES 
    (@INSERT_DATA_ROW_COUNTER + 1,
    'LAST' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'FIRST' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'TITLE' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'TITLE_OF_COURTESY' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    DATEADD(YEAR, -25, GETDATE()),
    DATEADD(YEAR, -21, GETDATE()),
    'ADDRESS' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'CITY' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'REGION' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    @INSERT_DATA_ROW_COUNTER + 100001,
    'COUNTRY' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 5551230001),
    0,
    'PHOTO' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    'NOTES' + CONVERT(VARCHAR(32), @INSERT_DATA_ROW_COUNTER + 1),
    1)
    SET @INSERT_DATA_ROW_COUNTER=@INSERT_DATA_ROW_COUNTER+1
END
SET NOCOUNT OFF
--end Insert some data

--This is your WHERE CLAUSE for the count and for the data retrieval
DECLARE @WHERE_CLAUSE VARCHAR(MAX)
SET @WHERE_CLAUSE='LASTNAME LIKE ''%0%'''

--This is the maximum number of rows that you would like to allow
DECLARE @MAX_ROW_COUNT int
SET @MAX_ROW_COUNT=400

DECLARE @COUNTROWS int
SET @COUNTROWS=-1
DECLARE @COUNT_SQL NVARCHAR(MAX)
SET @COUNT_SQL='select @COUNTROWS=count(*) from EMPLOYEES WHERE EMPLOYEE_ID IN (SELECT TOP ' + CONVERT(VARCHAR(64), @MAX_ROW_COUNT + 1) + ' EMPLOYEE_ID FROM EMPLOYEES WHERE ' + @WHERE_CLAUSE +')'
exec sp_executesql @COUNT_SQL, N'@COUNTROWS int output', @COUNTROWS output;
IF @COUNTROWS <= @MAX_ROW_COUNT
BEGIN
    DECLARE @SQL VARCHAR(MAX)
    SET @SQL='SELECT * FROM EMPLOYEES WHERE ' + @WHERE_CLAUSE
    EXEC (@SQL)
END
ELSE
BEGIN
    DECLARE @msg VARCHAR(255)
    SET @msg='The limit for this query is ' + CONVERT(VARCHAR(32), @MAX_ROW_COUNT) + ' rows. Change your query as required.'
    RAISERROR (@msg, 1, 1)
END
DROP TABLE EMPLOYEES
GO

基本上我希望能够定义从一个结构到另一个结构的字段映射以及转换函数,这样我就可以在不编写所有代码的情况下手动完成转换。

我想象的是,可能会有某种Go发生器可以做这样的事情。我认为使用反射是不可能的,因为嵌套结构(虽然我愿意接受建议)。

基本上,是否有人知道这样做的方法比手动编写转换代码或创建自己的生成器更好?

0 个答案:

没有答案