亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以從字符串創建比較運算符?

是否可以從字符串創建比較運算符?

Go
隔江千里 2023-08-07 18:53:54
我正在嘗試創建一個函數,該函數將從預定義的數組中生成 if 條件。例如:package errorstype errorCase struct {    // This is the field I need to get in another struct    Field        string    // The comparison operator    TestOperator string    // The value that the expected one should not with equal...    WrongValue   interface{}}var ErrorCases = []*errorCase{ {    "MinValue",    "<",    0,}, {    "MaxValue",    "==",    0,}}實際上,我用 for 循環創建了一個新函數,該函數迭代所有這些“錯誤情況”func isDirty(questionInterface models.QuestionInterface) bool {    for _, errorCase := range errors.ErrorCases {        s := reflect.ValueOf(&questionInterface).Elem()        value := s.Elem().FieldByName(errorCase.Field)        // At this point I need to create my if condition        // to compare the value of the value var and the wrong one        // With the given comparison operator    }    // Should return the comparison test value    return true}是否可以創建這樣的 if 條件?使用反射包?我認為這是可能的,但我不知道應該從哪里開始。
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

這個有可能。我以前構建過一個像這樣的通用比較庫。

簡單來說,比較包含三個部分:

  1. 某種值,位于比較的左側。

  2. 運算=<>, ...)。

  3. 某種值,位于比較的右側

這 3 個部分僅包含兩種不同的類型 - valueoperator。我試圖將這兩種類型抽象為它們的基本形式。

  • value可以是任何東西,所以我們使用空接口 - interface{}。

  • 運算符是有限集的一部分,每個運算符都有自己的規則。

type Operator int
const (
    Equals Operator = 1
    )

評估與符號的比較=只有一條有效規則 - 兩個值應該屬于同一類型。你無法比較1hello。之后,您只需確保這些值相同即可。

我們可以實現一個新的元類型來包裝評估operator.

評估與符號的比較=只有一條有效規則 - 兩個值應該屬于同一類型。你無法比較1和hello。之后,您只需確保這些值相同即可。


我們可以實現一個新的元類型來包裝評估operator.


// Function signature for a "rule" of an operator.

type validFn func(left, right interface{}) bool


// Function signature for evaluating an operator comparison.

type evalFn func(left, right interface{}) bool


type operatorMeta struct {

    valid []validFn

    eval  evalFn

}

現在我們已經定義了類型,我們需要實現 的規則和比較函數Equals。


func sameTypes(left, right interface{}) bool {

    return reflect.TypeOf(left).Kind() == reflect.TypeOf(right).Kind()

}


func equals(left, right interface{}) bool {

    return reflect.DeepEqual(left, right)

}

驚人的!因此,我們現在可以驗證兩個值是否屬于同一類型,如果是,我們可以將它們相互比較。難題的最后一部分是將運算符映射到其適當的規則和評估,并具有執行所有這些邏輯的函數。


var args = map[Operator]operatorMeta{

    Equals: {

        valid: []validFn{sameTypes},

        eval:  equals,

    },

}


func compare(o Operator, left, right interface{}) (bool, error) {

    opArgs, ok := args[o]

    if !ok {

        // You haven't implemented logic for this operator.

    }


    for _, validFn := range opArgs.valid {

        if !validFn(left, right) {

            // One of the rules were not satisfied.

        }

    }


    return opArgs.eval(left, right), nil

}

讓我們總結一下到目前為止我們所得到的:

  • 將基本比較抽象為運算符。

  • 創建了一種方法來驗證一對對于運算符是否有效。

  • 創建了一種在給定兩個值的情況下評估運算符的方法。

(去游樂場)

我希望我能對您如何解決這個問題提供一些見解。這是一個簡單的想法,但需要一些樣板才能正常工作。

祝你好運!


查看完整回答
反對 回復 2023-08-07
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號